Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: array assignment with list operator , no parenthesis

by Random_Walk (Parson)
on May 14, 2007 at 14:04 UTC ( #615308=note: print w/ replies, xml ) Need Help??


in reply to array assignment with list operator , no parenthesis

You are not copying an array into your array. You are putting in the result of the operation 7,2,3 which is 7 @one = 7,2,3 which due to precedence is evaluated as (my @one = 7),2,3 as previously explained in this thread. To get the array I think you want in there:

use strict; use warnings; use strict; my @one = (7,2,3); print "@one\n"; Output :- 7 2 3

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

update

shmem pointed out my sloppy summary of this problem. I was concentrating on what I thought he wanted to achieve and lost sight of the true explanation.


Comment on Re: array assignment with list operator , no parenthesis
Download Code
Re^2: array assignment with list operator , no parenthesis
by shmem (Canon) on May 14, 2007 at 14:10 UTC
    You are putting the result of the operation 7,2,3 which is 7.

    No, it is not. Consider:

    perl -le 'sub foo { 7,2,3 } $foo = foo(); print $foo' 3

    It's a precedence problem as other posters pointed out.

    update Well, actually... the above example is misleading, since the comma operator is used in list context here - subroutines always return lists (if not declared with a single ($) as prototype, that is). And evaluating a list in scalar context gives its last element - which is just what the comma operator does ;-) A better example might be

    print not 1,0; print $/; __END__ 1

    The not operator gets the 0 and turns it to 1.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-12-28 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls