in reply to Concise foreach expression

You are trying to combine postfix foreach and postfix if and running into two problems:

  1. they can't be chained
  2. they can't use loop-vars other than $_

Your best bet for a concise expressions to be chained are map and grep

print grep { /test/ } @names

update

N.B.: you are still limited to $_

update

which means you won't have much use of nested loops with map

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Concise foreach expression
by AppleFritter (Vicar) on Aug 29, 2014 at 16:43 UTC

    which means you won't have much use of nested loops with map

    Could you explain what you mean there? map localizes $_, so nested maps are not a problem. Code like the following (admittedly contrived) snippet works as expected ($_ is not clobbered by the inner map):

    my @list = ( ["Barney", "Rubble"], ["Fred", "Flintstone"], ["Betty", "Rubble"], ["Wilma", "Flintstone"] ); map { say "Outer map before: @$_"; map { 1 } @$_; say "Outer map after : @$_"; } @list;

    But perhaps there's other pitfalls, so I'd be grateful for any enlightenment.

      The problem happens when you need to use the $_ from the outer map in the inner map, as in the following contrived example:
      my %tree = ( child1 => {}, child2 => { grandchild1 => { brood3 => {} }, grandchild2 => { brood1 => {}, brood2 => {} }, }, ); say for 'Grandchildren:', map { my $x = $_; # <--- remove me! "$_: " . (ref $tree{$_} ? join ', ' => map keys %{ $tree{$x}{$_} }, keys %{ $tree{$_} } : q()) } keys %tree;
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      > Could you explain what you mean there?

      I was talking about chaining maps or fors to simulate nested foreach loops.

      I.o.w. list comprehensions are very hard to implement in Perl without nested blocks.

      e.g. try to implement this Python example for "Pythagorean triples" w/o nesting

      >>> [(x,y,z) for x in range(1,30) for y in range(x,30) for z in range( +y,30) if x**2 + y**2 == z**2] [(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, + 15), (10, 24, 26), (12, 16, 20), (15, 20, 25), (20, 21, 29)]

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Ah, yes, I see what you mean.