Problem with MODULO function?

Hi Peter,

I think it has to do with the fact that numberofpages is a string
instead of an integer. In the irb:

number_of_pages = 5
number_of_pages % 2 => 1
number_of_pages = '5'
number_of_pages % 2 => 5

So the test (numberofpages % 2 != 0) will always return a false, unless
numberofpages was 0 in the first place.

Workaround: use ( numberofpages.to_i % 2 ) instead.

Jan.

···

-----Original Message-----
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of Peter Bailey
Sent: 03 May 2006 13:14
To: ruby-talk ML
Subject: Problem with MODULO function?

The results of my little script here confuse me. I looking for an odd
number of pages in postscript files. When it's an odd number, I add a
blank, to make an even page count. In my test here, I've got 2 test
postscript files. One has 11 pages; the other has 42 pages. As can be
seen by my results below this script, it's seeing both as odd numbered,
and, it's adding a blank to each. Is my formula for MOD not correct?
I've tried it with "not equal to zero," like here, and, I've tried it
with "equal to one," with the same results.

Dir.chdir('c:/scripts/ruby/temp')
psfiles = Dir.glob('*.ps')
psfiles.each do |psfile|
  File.open(psfile, "a") do |writepage|
    File.read(psfile).scan(/\%\%Pages: (\d{1,5})\n/) do
      numberofpages = $1
      #Diagnostic to see the page counts of each file.
      puts numberofpages
      #If the page count is odd, then,
          #add a blank to make it an even page count.
      if (numberofpages % 2) !=0 then
        #Diagnostic--what is it seeing?
        puts "odd number of pages"
        writepage << "showpage\n"
      end
    end
  end
end

C:\scripts\RUBY\temp>c:\scripts\ruby\images\test4.rb
42
odd number of pages
11
odd number of pages

Thank you, anyone, who can help me with this. . . .

--
Posted via http://www.ruby-forum.com/\.

Yes, String#% does something completely different:

# => "16768.181"
"%.3f" % 16768.181202317
# => "16768.181"
"%d" % 502
# => "502"
"An example of %s with %s" % ["formatting", "String#%"]
# => "An example of formatting with String#%"

ri 'String#%'
--------------------------------------------------------------- String#%
     str % arg => new_str

···

On Wed, 2006-05-03 at 21:21 +0900, jan aerts (RI) wrote:

Hi Peter,

I think it has to do with the fact that numberofpages is a string
instead of an integer. In the irb:
> number_of_pages = 5
> number_of_pages % 2 => 1
> number_of_pages = '5'
> number_of_pages % 2 => 5

So the test (numberofpages % 2 != 0) will always return a false, unless
numberofpages was 0 in the first place.

------------------------------------------------------------------------
     Format---Uses _str_ as a format specification, and returns the
     result of applying it to _arg_. [...snipped...]

(P.s. seems to be a lot of top-posting going on these days :frowning: )

Workaround: use ( numberofpages.to_i % 2 ) instead.

Jan.

-----Original Message-----
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of Peter Bailey
Sent: 03 May 2006 13:14
To: ruby-talk ML
Subject: Problem with MODULO function?

The results of my little script here confuse me. I looking for an odd
number of pages in postscript files. When it's an odd number, I add a
blank, to make an even page count. In my test here, I've got 2 test
postscript files. One has 11 pages; the other has 42 pages. As can be
seen by my results below this script, it's seeing both as odd numbered,
and, it's adding a blank to each. Is my formula for MOD not correct?
I've tried it with "not equal to zero," like here, and, I've tried it
with "equal to one," with the same results.

Dir.chdir('c:/scripts/ruby/temp')
psfiles = Dir.glob('*.ps')
psfiles.each do |psfile|
  File.open(psfile, "a") do |writepage|
    File.read(psfile).scan(/\%\%Pages: (\d{1,5})\n/) do
      numberofpages = $1
      #Diagnostic to see the page counts of each file.
      puts numberofpages
      #If the page count is odd, then,
          #add a blank to make it an even page count.
      if (numberofpages % 2) !=0 then
        #Diagnostic--what is it seeing?
        puts "odd number of pages"
        writepage << "showpage\n"
      end
    end
  end
end

C:\scripts\RUBY\temp>c:\scripts\ruby\images\test4.rb
42
odd number of pages
11
odd number of pages

Thank you, anyone, who can help me with this. . . .

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk