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

Re^2: List assignment in scalar context

by dragonchild (Archbishop)
on Jan 10, 2005 at 18:50 UTC ( #421039=note: print w/replies, xml ) Need Help??


in reply to •Re: List assignment in scalar context
in thread List assignment in scalar context

That doesn't explain why the following happens:
my ($a,$b,$c); my $n1 = ($a,$b,$c) = split /,/, "1,2,3,4,5"; my $n2 = my @x = ($a,$b,$c) = split /,/, "1,2,3,4,5"; print "$n1 ... $n2\n"; ---- 4 ... 3

If it splits to one more, then why is that one going across one boundary, but not two?

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^3: List assignment in scalar context
by !1 (Hermit) on Jan 10, 2005 at 19:21 UTC

    Oh come off it.

    sub list { return 1..5 } my ($a,$b,$c); my $n1 = ($a,$b,$c) = list(); my $n2 = my @x = ($a,$b,$c) = list(); print "$n1 ... $n2\n"; ---- 5 ... 3

    When you have scalar = list = list, you will always get the count of items in the rightmost list in scalar. In your $n2 example, your array is getting three items from the list. It isn't a scalar, so it isn't getting the count. What else would be expected? The behavior that merlyn is referring to is the reason why you don't see the complete count in the scalar.

      So, based on what you're saying, $n2 should equal 5. Since @x isn't a scalar, it shouldn't affect the count of $n2 because I have scalar = list = list = list ... so, why doesn't that work?

      Not to mention, where is it documented that When you have scalar = list = list, you will always get the count of items in the rightmost list in scalar. ?? I don't remember ever reading that ...

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        = is right associative. Thus you have list = list (rightmost) before you get scalar = list = list. The list in question has 3 elements. Of course in this case, I think the behavior of an array in scalar context has more to do with the overall result than anything else. Note that if you replace the array declaration with any list of the form ($a,$b,...), you still get the count in $n2.

        perldoc perldata, look for "List assignment in scalar context". Note that the extra parentheses around the rightmost assignment in the example are superfluous since they don't affect the order of operations.

        Also, to those downvoting my nodes in this thread: if the reason that you're downvoting my nodes is due to the lack of technical merit of my posts, feel free to /msg me or reply.

        Since @x isn't a scalar, it shouldn't affect the count of $n2 because I have scalar = list = list = list

        No, you don't. You have scalar = array = list = list, which, as far as the scalar is concerned, reduces to scalar = array, after the array is assigned its value. An array is not a list. The Camel is quite clear on that point. It's also clear on what an array returns in scalar context. No surprises there.

Re^3: List assignment in scalar context
by Aristotle (Chancellor) on Jan 10, 2005 at 22:35 UTC

    You tricked yourself.

    In the first case, the list assignment is evaluated in scalar context. In the second case, the list assignment is evaluated in list context and assigned to @x, yielding an array with three elements, which is then evaluated in scalar context. And an array with three elements evaluates to 3 in scalar context.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-09-26 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?