List assignment to array affects scalar results

Discussion in 'Perl Misc' started by Mark, Mar 16, 2011.

  1. Mark

    Mark Guest

    Why does the introduction of the assignment to @b affect the value of
    $cnt?

    use strict;
    use warnings;

    my $cnt;
    my($x,$y,$z);
    my(@ary) = qw(a b c d e);


    $cnt = ( ($x,$y,$z) = @ary );
    print "cnt=$cnt\n"; # produces 5

    my @b;
    $cnt = ( @b = ($x,$y,$z) = @ary );
    print "cnt=$cnt\n"; # produces 3
     
    Mark, Mar 16, 2011
    #1
    1. Advertising

  2. Mark

    C.DeRykus Guest

    On Mar 16, 3:39 pm, Mark <> wrote:
    > Why does the introduction of the assignment to @b affect the value of
    > $cnt?
    >
    > use strict;
    > use warnings;
    >
    > my $cnt;
    > my($x,$y,$z);
    > my(@ary) = qw(a b c d e);
    >
    > $cnt = ( ($x,$y,$z) = @ary );
    > print "cnt=$cnt\n"; # produces 5
    >
    > my @b;
    > $cnt = ( @b = ($x,$y,$z) = @ary );
    > print "cnt=$cnt\n"; # produces 3


    See: perldoc perldata and its explanations
    of lists and list vs. scalar context

    Specifically, from that doc:

    List assignment in scalar context returns the
    number of elements produced by the expression
    on the right side of the assignment:

    $x =($foo,$bar) = (3,2,1));# set $x to 3, not 2
    ...

    Similarly, in your first case, that evaluates to 5.
    The eventual array assignment to @b in the second
    case is also evaluated in scalar contest. The first
    three elements in @ary are assigned to ($x,$x,$z)
    and the additional elements in @ary are discarded.
    @b is then assigned ($x,$y,$z) and then is evaluated
    in scalar context to yield 3.

    --
    Charles DeRykus
     
    C.DeRykus, Mar 17, 2011
    #2
    1. Advertising

  3. Mark wrote:
    > Why does the introduction of the assignment to @b affect the value of
    > $cnt?


    It is not the array that does it, it is the extra assignment.

    > use strict;
    > use warnings;
    >
    > my $cnt;
    > my($x,$y,$z);
    > my(@ary) = qw(a b c d e);
    >
    >
    > $cnt = ( ($x,$y,$z) = @ary );


    The list assignment is evaluated in a scalar context.

    > print "cnt=$cnt\n"; # produces 5
    >
    > my @b;
    > $cnt = ( @b = ($x,$y,$z) = @ary );


    The initial list assignment is evaluated in a *list* context, not a
    scalar context. The magic only applies to list assignment in a scalar
    context.

    You get the same thing using a non-array:

    $cnt = ( ($x1,$x2,$x3) = ($x,$y,$z) = @ary );
     
    Xho Jingleheimerschmidt, Mar 18, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Williams Williams
    Replies:
    2
    Views:
    139
    Stefano Crocco
    Sep 28, 2010
  2. Clint Olsen
    Replies:
    6
    Views:
    407
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. fatted
    Replies:
    1
    Views:
    115
    John W. Krahn
    Nov 26, 2004
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    193
    Arndt Jonasson
    Jan 27, 2005
  5. 490
    Replies:
    1
    Views:
    97
    Mumia W.
    Mar 27, 2007
Loading...

Share This Page