Suggestions for 2002

Tim Peters tim.one at home.com
Sun Jan 13 03:05:27 EST 2002


[Paul Rubin]
> ...
> To get even more confusing, in Perl,
>
>     @a = qw(cat dog);
>     $i = 1;
>     $i, $a[$i] = 0, "boo";
>     print join(' ', at a),"\n";
>
> prints "cat 0".  I'm not sure how it came up with that.

If you run Perl with -w, it should complain twice:  the third line isn't an
assigment expression, it's a 3-term comma expression whose middle term is
the assigment "$a[$i] = 0".  The things on either side of it ($i and "boo")
are effectively nops (and -w warns about both).

Change it to

    ($i, $a[$i]) = (0, "boo");

to get your intent.  Then it prints "cat boo" (on my box, anyway).  I like
Python's answer better, at least for Python:  unlike Perl, Python doesn't
have explicit references, and you need (as you found out <wink>) a
"pointerish" concept to explain the answer you prefer.  Python's answer is
what you get if you follow your *original*, straightforward, pointer-free
attempt to explain how it "should be" implemented.





More information about the Python-list mailing list