Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: array assignment with list operator , no parenthesis

by Random_Walk (Prior)
on May 14, 2007 at 14:04 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: array assignment with list operator , no parenthesis
by shmem (Chancellor) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://615308]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found