From: dblack@... Date: 2007-06-06T21:27:42+09:00 Subject: Re: efficient regex scanning Hi -- On Wed, 6 Jun 2007, Robert Klemme wrote: > On 06.06.2007 13:08, dblack@wobblini.net wrote: >> Hi -- >> >> On Wed, 6 Jun 2007, Trochalakis Christos wrote: >> >>> Hello there, >>> >>> I wan't to extract all the words from a file and so i wrote the >>> following code: >>> >>> file = ARGV[0] >>> File.open('output','w') {|f| >>> IO.read(file).scan(/\w+/).each{|w| f.print w} >>> } >>> >>> The problem with this code is that it stores all the words in an array >>> which is not so good in terms of efficiency. >>> Is there a better way to do it? >>> Something like IO.read(file).each_scan { foo } >> >> You could do something like this (untested, and reversing your logic >> somewhat): >> >> File.open(file).each {|line| f.print(line.scan(/\w+/)) } >> >> (You might want to join them with a space or something so they don't >> all run together.) > > You're not closing the IO. I know it's not an issue for a small script > but... It's not a complete script; I was only showing one line. At the very least it's not going to run unless you assign something to f :-) David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)