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

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;

Replies are listed 'Best First'.
Re^3: Basic list manipulation, and a bit of map() confusion...
by Corion (Patriarch) on Feb 24, 2008 at 19:25 UTC

    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 ().

        One of the items the OP processes is 'five'. That item does not match (does not contain 'x'), so tr/// and s/// would both be "false" as far as the ternary is concerned (and so would be removed from the list in your code). Nevertheless, the OP does what that element in the resulting list because it's not an empty string after the replacement happens.