From: Mike Fletcher Date: 2006-05-03T22:20:04+09:00 Subject: Re: Problem with MODULO function? Peter Bailey wrote: > 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 The variable numberofpages contains a String instance, not a number. You're calling String#% which is the sprintf-like formatting operator, not Fixnum#% which is the modulo operator. So "42" % 2 is the String "42", which is not going to be equal to numeric 0. You want to convert the string to a number with numberofpages = Integer( $1 ) -- Posted via http://www.ruby-forum.com/.