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

Hi Perlmonks.. Is it possible to map not just one array before the grep function?? In this case I want to print the elements in the array which contains the word 'EMBL' :
print OUT join "\n", map {${$_}[1]}grep { ${$_}[0] eq "EMBL" } $entry- +>DRs->elements;
In this case what if I want to map ${$_}[1] and ${$_}[2] both at the same time?? I try using
print OUT join "\n", map {${$_}[1],${$_}[2]}grep { ${$_}[0] eq "EMBL" +} $entry->DRs->elements;
But either comma or colon, they still give me some error. And what if I want to map all the elements in the array?? I have tried without the map function such as
print OUT join "\n",grep { ${$_}[0] eq "EMBL" } $entry->DRs->elements;
But it doesn't print the content of the array but only the address : ARRAY(...). Thank you in advanced.

Edit Masem 2002-01-17 - Added code tags, changed title from "about grep..."

Replies are listed 'Best First'.
Re: about grep....
by Zaxo (Archbishop) on Jan 17, 2002 at 15:17 UTC

    $ntry->DRs->elem does not look like any kind of array. Do you mean that $entry is a Hash of Hashes of Hashes of Arrays? The innermost array reference would then be obtained by:$entry->{'DRs'}->{'elem'}, and dereferenced to an array by @{$entry->{'DRs'}->{'elem'}}. It would be helpful for you to describe you data and your intentions.

    Within grep's block, it looks like you are expecting an array reference in $_, but again the syntax is incorrect (well, odd to me ;-). I think you want { $_->[0] eq 'EMBL' } there.

    As it is, grep is not working because you are giving it an incorrect code block, and a single element list which has incorrect syntax. I think if you clarify what data you have built, the solution will pop right out at you.

    Update: Corrected myself, as shown. ++fever.

    After Compline,
    Zaxo

Re: about grep....
by djantzen (Priest) on Jan 17, 2002 at 15:44 UTC

    This worked fine for me:

    @array = (['EMBL','a','b'],['c','d'],['EMBL','e','f']); $list = join "\n", map {${$_}[1],${$_}[2]}grep { ${$_}[0] eq "EMBL" } +@array; print $list;
    This prints 'abef' separated by newlines.

      Hi. While your solution appears to be correct the syntax you have chosen is, well, odd enough that I went cross-eyed trying to read it..

      So heres my simplification

      #Initial code... @array = (['EMBL','a','b'],['c','d'],['EMBL','e','f']); $list = join "\n", map {${$_}[1],${$_}[2]}grep { ${$_}[0] eq "EMBL" } +@array; print $list; #first step convert the ${$_}[$index] to $_->[$index] $list = join "\n", map { $_->[1],$_->[2] } grep { $_->[0] eq "EMBL" } +@array; #second step convert to list slice $list = join "\n", map { @{$_}[1,2] } grep { $_->[0] eq "EMBL" } @arra +y; #next step simplify map{}grep{} into just map{} $list = join "\n", map { $_->[0] eq "EMBL" ? @{$_}[1,2] : () } @array +; print "\nSimplified:\n$list";
      Usually (yes there are exceptions) map {} grep{} or grep{} map{} can be simplified into
      map { (grep_condition) ? map_function : () }
      With greater efficiency and easier understanding (er on the latter IMO :-).

      Yves / DeMerphq
      --
      When to use Prototypes?