Robert Dober
7/11/2007 9:27:00 AM
On 7/11/07, Stefano Crocco <stefano.crocco@alice.it> wrote:
> 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
That is nice code, I did not want to rely on the exact format of the
input as I learned to distrust specifications ;). But you do what OP
asked for.
Allow me to recall a small hint David Black has given some time ago
puts a
is equivalent to
puts a.join("\n")
>
> 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
>
>
Cheers
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck