From: Peter Bailey Date: 2006-04-13T00:24:22+09:00 Subject: Re: Simple substitutions Ross Bamford wrote: > On Wed, 2006-04-12 at 22:35 +0900, Peter Bailey wrote: >> > if pages >> > File.open("psout.txt", "w") { |out| out << "Pages: #{pages}" } >> > end >> >> >> Aha. I didn't know about "scan." Just the word alone makes sense for >> what I want. And, yes, I found it in the Big Book, on page 617. >> >> What does the "break" do, Ross? Is it just telling RUBY to stop if it's >> found what it was scanning for? > > Yes - you can find a bit about it near the end of this page: > > http://www.rubycentral.com/book/tut_expressions.html > > Looking at that again, though, I can't imagine why I didn't just write: > > if File.read('test1.ps') =~ /\%\%Pages: ([0-9]{1,5})/ > pages = $1 > end > > For just a single match. Yes, the =~ notation certainly seems simpler. Now, I'm pursing this further and now I need to look for blank pages in my postscript. These are found with a regex, too, a more complex one, but, I've created one that works. And, with this search, because there may be more than just one, I've created an array and I'm pushing every page number I find blank into that array. Cool. I love this stuff. I'm having a bit of trouble though printing a simple array.length statement. Please check this out: ************************************************ Dir.chdir('c:/scripts/ruby/temp') blanks = [] File.read("test2.ps").scan(/\%\%Page: [\d()]+ (\d{1,5})\n\%\%PageBoundingBox: \d{1,5} \d{1,5} \d{1,5} \d{1,5}\n\%\%PageOrientation:/) do blanks.push($1) number = blanks.length blanks.push(" ") #break end if blanks File.open("psout2.txt", "w") { |out| out << "Blank Pages in This PDF: #{blanks}\nNo. of Blanks: #number\n" } end ************************************************ I'm getting this, which I'm extremely proud of, but, it doesn't quite make it: +++++++++++++++++++++++++++++++++++++++++ Blank Pages in This PDF: 46 68 72 80 83 No. of Blanks: #number +++++++++++++++++++++++++++++++++++++++++ Thanks. If I can just get this, then, I'll streamline my script to take advantage of the simple match, like you suggested. -- Posted via http://www.ruby-forum.com/.