in reply to Basic list manipulation, and a bit of map() confusion...

Personally I would use a grep before or after the map, but when you want to return a varying number of elements from map you can use the ternary operator:

my @b = map { s/x//; # this could be written more efficient as tr[x][]d; $_ ? $_ : () } @a;

Replies are listed 'Best First'.
Re^2: Basic list manipulation, and a bit of map() confusion...
by jwkrahn (Abbot) on Feb 24, 2008 at 19:17 UTC
    s/x//; # this could be written more efficient as tr[x][]d;

    The difference being that  s/x// removes one 'x' character while  tr[x][]d removes all 'x' characters.    To be fair, the OP's code used s/x//g instead   :-)

    And you don't need two statements to accomplish that:

    my @b = map s/x//g ? $_ : (), @a; # Or: my @b = map tr/x//d ? $_ : (), @a;

      Not exactly:

      perl -le "print for map s/x//g ? $_ : (), @ARGV" 1 2 3x 4xx xxx 6x 3 4 6
      perl -le "print for map tr/x//d ? $_ : (), @ARGV" 1 2 3x 4xx xxx 6x 3 4 6

      The return value of s/// and tr[] is the number of substitutions made, not the modified string (unfortunatley).

        Correct, the return value of s/// and tr[][] is the number of substitutions made.    However you are not returning the return value of s/// or tr[][], you are returning either the value of $_ or the empty list ().