in reply to Mapping elements returned from a grep statement

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.

Replies are listed 'Best First'.
Re: Re: about grep....
by demerphq (Chancellor) on Jan 17, 2002 at 16:44 UTC
    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?