From: Robert Dober Date: 2007-07-11T20:57:46+09:00 Subject: Re: if / else, how to include next line On 7/11/07, Marc Hoeppner wrote: > Hm, maybe I was in fact a bit vague here, sorry. > > I think grep will do the job (sometimes its just simpler than one > expected...), but now just out of curiosity: > > I have one file, structured as shown above. It contains some 600 > "entries" (that is a KOG-number followed by 8 lines of information). Out > of these 600-someting entries, I only need a particular subset. The > information on which KOG-entries I need are in a second text file > (basically the KOG-numbers, one in each line), like so: > > KOG0019 > KOG0101 > KOG0245 > etc > > Ideally,the program would read this keyword-file, store the information > in a variable (an array, I guess) and then use this array to identify > the entries in the "main" file and puts them into stdout. > > *scratches head* > Let us assume you call your prog like this ruby extractor.rb keynames = File.readlines( ARGV.shift ) key_hash = {} # a set keynames.each do | line | key_hash[line.strip] = true end File.open(ARGV.first){ ## Preferable to assure automatic closure of file | f | ## that is the file we opened count =0 f.each { |line| ... if key_hash[ line.strip ] then puts line count = 8 end } } and maybe stick to "{...}" or "do ... end" just wanted to show you both Cheers Robert } } > > -- > Posted via http://www.ruby-forum.com/. > > -- I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby -- Kent Beck