From: dblack@... Date: 2003-08-01T21:54:51+09:00 Subject: Re: A bundle of newbie queries Hi -- On Fri, 1 Aug 2003, Brian Candler wrote: > On Fri, Aug 01, 2003 at 09:30:19PM +0900, dblack@superlink.net wrote: > > If you just want the count of lines with plastic_1.1..., you could do: > > > > plastic_count = file.readlines.grep(/plastic_1.1.../).size > > > > or, to save reading the whole file in at once: > > > > plastic_count = 0 > > file.readlines.each do |line| > > plastic_count += 1 if /plastic_1.1.../.match(line) > > end > > That example also reads in the entire file: that's what 'readlines' does :-) > > But File is an Enumerable object. Hence you can do > > plastic_count = file.grep(/plastic_1.1.../).size > > (which will generating an intermediate array of matching lines only), or > > plastic_count = 0 > file.each do |line| > plastic_count += 1 if /plastic_1.1.../.match(line) > end > > (which generates no intermediate array at all) Yikes. I seem to have readlines disease. Thanks for the corrections. David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav