in reply to Re: Howto strip 42 from comma separated list using s///
in thread Howto strip 42 from comma separated list using s///

Hi,
sorry, but just for me to understand:
$1 && $2
means: if both are there, return the latest, in this case $2?
Like this:
perl -e'$a=1;$b=2;print ($a && $b)'
Thanks,
svenXY

Replies are listed 'Best First'.
Re^3: Howto strip 42 from comma separated list using s///
by japhy (Canon) on Nov 03, 2005 at 13:37 UTC
    It's a trick, I'll admit. In Perl, && returns the right-hand operand if the left-hand one is true, and the left-hand operand if the left-hand one is false. X && Y returns X if X is false, and Y if X is true (regardless of Y's truth value!).

    Thus, in my regex, a comma is only inserted if both $1 and $2 are true. $1 && $2 is only true when $1 and $2 have commas in them (otherwise, at least one of them is empty, and thus the empty string is returned).


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart