From: "David A. Black" Date: 2009-06-26T05:01:57+09:00 Subject: Re: How do I get an integer from an array? Hi -- On Fri, 26 Jun 2009, Peter Bailey wrote: > Brian Candler wrote: >> Alex wrote: >>> The index method of the array class is a method just like any other, so >>> you >>> could just as well write it like: >>> >>> foo.[](0).[](0) >>> >>> which calls the `[]' method on the result of the previous call of the >>> `[]' >>> method, which is an array. >>> >>> foo[0] is just a shortcut ruby gives you for calling foo.[](0) >> >> Or in this particular case, you can do >> >> foo.first.first > > Well, interestingly, I've succeeded in some of my scripts. But, in this > one, it fails. It displays a few hundred filenames with page counts, > but, in this directory, there are literally thousands of PDF files. So, > it does a bunch, then dies. > > Dir.glob("*.pdf").each do |pdffile| > pages = `pdfinfo #{pdffile}` > pages = pages.scan(/^Pages:[ ]{2,99}([0-9]+)/) > pages = pages[0][0].to_i > puts "#{pdffile} #{pages}" > end > > I get: > ================ NoMethodError ===================== > C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:8:in `block in >
' > pages = pages[0][0].to_i > C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:5:in `each' > Dir.glob("*.pdf").each do |pdffile| > C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:5:in `
' > Dir.glob("*.pdf").each do |pdffile| > > ============================================= > Exception: undefined method `[]' for nil:NilClass That means that somewhere along the line, the scan operation isn't finding what you expect it to. Is it possible that you have a document with more than 99 occurrences of [] in a row? I'd still recommend trying the technique I suggested in my earlier answer. Getting a nested array of one element and unnesting it seems like the long way around. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com