From: Stefano Crocco Date: 2007-07-11T17:53:55+09:00 Subject: Re: if / else, how to include next line Alle mercoled狸 11 luglio 2007, Marc Hoeppner ha scritto: > Hi everyone, > > absolute beginner here, so bare with me ;) > > The following issue came up and I can't figure out how to solve. > Probably nothing very complicated, tho. > > I want to load a text file via ARGV[0] and then search it for a key > word. If this key word is found, it should be printed (puts) and...and > this is very I am stuck ...it should also included the next 8 lines. The > file structure looks like this: > > KOG0003 > Sc 000000000 > Dr 000000000 > Ar 001010011 > Ca 000100100 > Ho 001010010 > Sa 010000000 > An 100000100 > Pl 000001000 > KOG0009 > Sc 000100 > Dr 100000 > Ar 001001 > Ca 101000 > Ho 101010 > Sa 010000 > An 100000 > Pl 001000 > > So lets say I want to select for a specific KOG (or a list of KOGs) and > write them together with the associated lines into a new file. I guess > the issue at the moment is, that when I open the ARGV[0], it reads one > line at a time - cant quite figure out how to include the "go to next > line and puts it, too" part. > > Any help pointing me in the right direction would be greatly appreciated > > /Marc You can try this: require 'enumerator' text.each_slice(9) do |a| puts a.join "\n" if a.first == key end This will pass the array the lines grouped in arrays of 9 lines each (i.e, in the first iteration the array will contain the lines from KOG0003 to Pl 000001000, the second time from KOG0009 to Pl 001000, and so on). The line with the key word will be the first of each array, so you print the array only if it is equal to the key you chose. I hope this helps Stefano