From: Peter Bailey Date: 2006-10-14T03:31:06+09:00 Subject: Re: Regex parsing against filenames Hugh Sasse wrote: > On Sat, 14 Oct 2006, Peter Bailey wrote: > >> parse true against "pq181b" and "pq192b??" >> >> Here's a snippet of my code, showing where I'm doing the regex. All the >> filenames are in the variable $psfile. >> >> ... >> db = KirbyBase.new { |d| d.path = "E:/workflows/graphics/pagecounts" } >> pageinfo_tbl = db.get_table(:pageinfo) >> >> result = pageinfo_tbl.select { |r| $psfile =~ Regexp.new(r.dbfilename) } >> ... > > Without seeing the result of the r.dbfilename call it's hard to tell. > > result = pageinfo_tbl.select do |r| > dbf = r.dbfilename > re = Regexp.new(dbf) > > if re.match($psfile) > true > else > puts "#{r}.dbfilename => #{dbf} => #{re} which failed to match > #{$psfile}" > false > end > end > > might tell you something. >> >> >> Thanks, >> Peter >> > Hugh Thanks, Hugh. I tried your code snipped and it gave me data for every single row in my database, all 200 of them. So, it's kind of hard to read, because it display every field of every row. I've tried to simply things by simply using the .match operator, like this: result = pageinfo_tbl.select { |r| $psfile.match(r.dbfilename) } When I run this .match operation in IRB, using the 9 filenames I have, it matches every time! Here's my whole script now. I hope it's not too huge for this forum. require 'kirbybase' Dir.chdir("E:/workflows/graphics/indexes/VAXin_6x9") psfiles = Dir.glob("*.ps") psfiles.each do |$psfile| File.rename($psfile, $psfile.downcase) file_contents = File.read($psfile) file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do $totalpages = $1 if ($totalpages.to_i % 2) !=0 then file_contents << "\%\%Blank page from Asura\nshowpage\n" File.open($psfile, "w") { |f| f.print file_contents } $totalpages = $totalpages.to_i + 1 end db = KirbyBase.new { |d| d.path = "E:/workflows/graphics/pagecounts" } pageinfo_tbl = db.get_table(:pageinfo) result = pageinfo_tbl.select { |r| $psfile.match(r.dbfilename) } if result.empty? #send an e-mail if empty system("blat E:/live/scripts/messages/blank_page.txt -s \"#{$psfile} is not a known Index filename. Check with Accounting on this.\" -to prodadmin@bna.com -f \"BNA Internal Production Mail Server\" -q") else $count = result.first.pagecount puts "#{$psfile}, #{$count}, #{$totalpages}" #just for diagnostics pageinfo_tbl.update(:pagecount=>$totalpages.to_i + $count.to_i) { |r| r.recno == result.first.recno } end File.rename($psfile, $psfile.downcase) end -- Posted via http://www.ruby-forum.com/.