From: "David A. Black" Date: 2009-06-25T02:07:59+09:00 Subject: Re: How do I get an integer from an array? Hi -- On Thu, 25 Jun 2009, Peter Bailey wrote: > Hi, > I need to process individual pages of PDFs. To do so, I need to get the > page count of the PDF, then, do some image magic with each page of that > PDF. So, first thing I do is use a utility that gives me that page > count. I get the page count, but,it's an array. And, it doesn't let me > treat that "array" as a number, so, I can't do what I want. Here's a > snippet of my script and what I get with it. Thanks. > > Dir.chdir("N:/infoconpdf") > file = "ehs-X7917735.pdf" > pages = `pdfinfo #{file}` > pages = pages.scan(/^Pages:[ ]{2,99}([0-9]+)/) > puts pages > 1.upto(pages) do |n| > puts n > end > > I get this: > 78 > ================ ArgumentError ===================== When you do a scan where the regex has parentheses, you get an array for each scan through the string, with the captures as elements of that array. So you end up with an array of arrays: >> string = "Hello. I am a string." => "Hello. I am a string." >> string.scan(/( \w )/) => [[" I "], [" a "]] So you got back [["78"]], I believe. You have to dig the number out. Another way to do it is: pages[/Pages:\D+(\d+),1/] (plus .to_i to convert it to an integer). 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