in reply to Re: "foreach" is to "next" as "map" is to ???
in thread "foreach" is to "next" as "map" is to ???

Abigail,
If you use a ternary where it branches to the empty list or a do block for each place there is a next statement, then I will buy it doing the same thing.
# contrived example my @paragraph; for ( @lines ) { next if /^#/; $_ = lc $_; $_ =~ tr/ -~//cd; next if /foobar/; $_ =~ s/administration/admin/g; next if length $_ > 60; push @paragraph, $_; }
This IMO would be insane for a map block. I stand corrected on the ternary/empty list/do block being functionally equivalent though in sacked's original example there was no do block.

Cheers - L~R

Replies are listed 'Best First'.
Re: "foreach" is to "next" as "map" is to ???
by Abigail-II (Bishop) on May 26, 2004 at 17:23 UTC
    though in sacked's original example there was no do block.
    Well, of course there wasn't a do block, because he was showing a foreach loop, and was asking how to do the equivalent using map. You don't need to do {} in the foreach.

    But sacked's original example didn't contain multiple nexts. There's also another way of dealing with this problem: a bare block:

    my @paragraph = map {;{ next if /^#/; $_ = lc $_; $_ =~ tr/ -~//cd; next if /foobar/; $_ =~ s/administration/admin/g; next if length $_ > 60; $_ }} @lines;

    Abigail

      There's also another way of dealing with this problem: a bare block:

      now that's cool

      Cheers - L~R
Re: Re: Re: "foreach" is to "next" as "map" is to ???
by diotalevi (Canon) on May 26, 2004 at 17:22 UTC

    I abhor foreach/push loops. Gag me with a spoon!

    my @paragraph = map { local $_ = lc; tr/-~//cd; /foobar/ ? () : do { s/administrator/admin/g; length() > 60 ? () : $_ } } grep /^#/, @lines;

    That code was ugly and I wouldn't have actually written that.

    my @paragraph = map { if ( /^#/ ) { () } else { local $_ = lc; tr/-~//cd; if ( /foobar/ ) { (); } else { s/administration/admin/g; length() > 60 ? () : $_; } } /^#/ ? do { } : (); } @lines;