agustina_s has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks.. I want to ask concerning this code below:
print OUT join ";", map {${$_}[1],${$_}[2]} grep { ${$_}[0] eq "EMBL" +} $entry->DRs->elements(); INPUT : DR EMBL;ABC;DEF;-. DR EMBL;GHI;JKL;- DR PIR;MNO;PQR;- OUTPUT : EMBL ABC;DEF;GHI;JKL;
Basically the $entry->DRs->elements() will return a reference to a list called DR.. in this case we reffered to the list whose first element=EMBL. Is there any way to make my output looks like the input... by modifying the map and grep function above? I tried to separate the map into two parts :
print OUT join ";", map {${$_}[1]}grep { ${$_}[0] eq "EMBL" } $entry-> +DRs->elements(),map {${$_}[2]} grep { ${$_}[0] eq "EMBL" } $entry->DR +s->elements(),"\n";
But the print out is still the same... Thanks in advanced...

Replies are listed 'Best First'.
Re: put only 2 elements in a line
by chromatic (Archbishop) on Jan 18, 2002 at 12:38 UTC
    join will happily slurp up your list. You need a loop of some kind. How's this?
    foreach (grep $_->[0] eq 'EMBL') { print OUT "DR EMBL;$_->[0];$_->[1];-.\n"; }
    Sorry, no map in there, but I don't think you need it. :)

    Update: Juerd caught a typo. Fixed!

Re: put only 2 elements in a line
by jryan (Vicar) on Jan 18, 2002 at 13:00 UTC

    Chromatic gave an excellent answer, but to answer your original question, this should work:

    print OUT map {"DR EMBL: ", join(';',(${$_}[1],${$_}[2])), "\n"} grep +{ ${$_}[0] eq "EMBL" } $entry->DRs->elements();
    This will add 3 elements to the list that is the return value of map:
    1. The string "DR EMBL: " (using ${$_}[0] here isn't necessary since the rest of the elements except for EMBL have been filtered out using the grep)
    2. A string created by joining the elements ${$_}[1] and ${$_}[2] with a semicolon.
    3. And finally, a newline

    I tried to follow the specifications that you wanted, but they weren't exactly clear... If you need something more, just ask.