Suggestions for 2002

Dave Tweed dtweed at acm.org
Sun Jan 13 09:22:50 EST 2002


Paul Rubin wrote:
> 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.

Perl doesn't have multiple assignment. That statement is simply
a comma-separated sequence of three expressions (the precedence
of = is higher than ,).

Perl first evaluated "$i" in void context (if you had enabled
warnings, it would have said something about that), then performed
the assignment "$a[$i] = 0" and then finally evaluated '"boo"' in
void context.

However, Perl does have list assignment:
   ($i, $a[$i]) = (0, "boo");
This prints "cat boo".

-- Dave Tweed



More information about the Python-list mailing list