Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Please explain 'list' assignment.

by friedo (Prior)
on Nov 03, 2006 at 19:10 UTC ( [id://582141]=note: print w/replies, xml ) Need Help??


in reply to Please explain 'list' assignment.

Because list assignments are parallel. A list of three items on the left expects three items on the right. Since there's only one, $var2 and $var3 remain undefined.

You can do what you want with:

my ($var1, $var2, $var3) = "Hello" x 3;

Update: Oops - I read your question slightly wrong. To assign the same value three times, use

my ($var1, $var2, $var3) = map "Hello", 1..3;
or
my ($var1, $var2, $var3) = ("Hello") x 3;
(thanks Sidhekin)

Replies are listed 'Best First'.
Re^2: Please explain 'list' assignment.
by shenme (Priest) on Nov 03, 2006 at 19:19 UTC
    The 'x' operator can act upon strings or upon lists. The use above, "Hello" x 3" created three concatenated copies of the string "Hello", thus "HelloHelloHello". What you wanted was "three copies of an item in list context", like so:
    my ( $var1, $var2, $var3 ) = ("Hello") x 3;
    The parentheses force the value into list context, the 'x' then makes three copies of that single value list, and you get ("Hello", "Hello", "Hello"), which is what OP wanted.
Re^2: Please explain 'list' assignment.
by Bagarre (Sexton) on Nov 03, 2006 at 19:20 UTC
    Just came back to say that didnt work :) The map works... and makes sense.
    Entities should not be multiplied unnecessarily.
Re^2: Please explain 'list' assignment.
by Bagarre (Sexton) on Nov 03, 2006 at 19:12 UTC
    Perfect. Thank you :)
    Entities should not be multiplied unnecessarily.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://582141]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found