Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > List assignment to array affects scalar results

Reply
Thread Tools

List assignment to array affects scalar results

 
 
Mark
Guest
Posts: n/a
 
      03-16-2011
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
 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      03-17-2011
On Mar 16, 3:39*pm, Mark <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      03-18-2011
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 );


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Use of initialized value in scalar assignment" kj Perl Misc 2 01-02-2008 08:28 PM
Can't modify constant item in scalar assignment emrefan Perl Misc 4 06-05-2007 03:21 AM
Assignment from scalar to user-defined type Richard Cavell C++ 2 03-06-2005 09:26 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments