### list assignment to list in scalar context

 on Nov 09, 2012 at 02:58 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Why does a list assignment to a list in scalar context return the number of elements on the right side list, but NOT the last element of the left side list that has been assigned values?

I know that:

```scalar((1,3,5))     #evaluates to the last element of the list --- 5
if( (5,3,0) )       #evaluates to the last element of the list --- 0,
+thus evaluates to FALSE
\$val=(5,6,7);       #evaluates to the last element of the list --- 7

and that:

```\$ret = ((\$i, \$j, \$k)=(5,6,8,9));        #evaluates to the number of el
+ements on the right side list --- 4
if( (\$k, \$v) = ( 1, 0 ) )               #evaluates to the number of el
+ements on the right side list --- 2, thus evaluates to TRUE
while ( (\$key, \$value) = each %map )    #still evaluates to the number
+ of elements on the right side hash

Now check a scalar assignment to a scalar in scalar context: if( \$i = 0 ) which will evaluate to the value of \$i on the left side, thus evaluate to 0, thus evaluate to FALSE

but consider an example of list assignment above:

```if( (\$k, \$v) = ( 1, 0 ) )
To me, it seems like it should evaluate to the value of list (\$k, \$v) on the left side just like \$i above, thus should evaluate to the last element of list (\$k, \$v) --- \$v, thus evaluate to 0 and at last should evaluate to --- FALSE, although I know the correct answer is it will evaluate to true.

So, the question is: for what purpose are the designing principles of scalar assignment and list assignment both in scalar context completely different? Just for convenience of usage? Isn't that a bit confusing?

Replies are listed 'Best First'.
Re: list assignment to list in scalar context
by TGI (Parson) on Nov 09, 2012 at 03:33 UTC

I can't really answer "why" Larry and crew chose the things they did. But, I might be able to shed a little light.

```scalar((1,3,5))     #evaluates to the last element of the list --- 5
if( (5,3,0) )       #evaluates to the last element of the list --- 0,
+thus evaluates to false
\$val=(5,6,7);       #evaluates to the last element of the list --- 7

These use the comma operator ',' in scalar context, returning whatever is on the right of the comma. In the case of multiple commas, you get the value of the rightmost expression.

There is no list here at all. You just think there is because you have been trained to think of the comma as meaning 'list'.

```\$ret = ((\$i, \$j, \$k)=(5,6,8,9));        #evaluates to the number of el
+ements on the right side list --- 4
if( (\$k, \$v) = ( 1, 0 ) )               #evaluates to the number of el
+ements on the right side list --- 2, thus evaluates to TRUE
while ( (\$key, \$value) = each %map )    #still evaluates to the number
+ of elements on the right side hash

Here we actually have some lists, and thus actual list assignment.

List assignment is scalar context gives us the size of the list that was passed into the assignment. So here \$foo = (\$bar, \$baz) = (1..50); \$foo is equal to 50.

In list context you get the contents of the resulting list. So  @foo = () = (1..50) leaves @foo empty.

Why is it this way? I don't know. But I can tell you that getting the size of the assigned from list can be useful.

```my \$count_foo = () = /foo/g;

Written a bit more compactly, that construct gets called "the goatse operator": \$foo =()= /foo/g; If you don't get the joke, don't look it up.

I hope this helps a bit.

TGI says moo

Re: list assignment to list in scalar context (boolean)
by tye (Sage) on Nov 09, 2012 at 03:45 UTC

List assignment in a scalar context returns the number of items in the right-hand side because that is extremely useful as a Boolean test, as shown in your own example:

```    while( my( \$k, \$v ) = each %h ) {

You wouldn't want that loop to terminate early just because one of the values in %h was false.

- tye

Re: list assignment to list in scalar context
by Anonymous Monk on Nov 09, 2012 at 15:45 UTC
Ok, here goes.

scalar((1,2,3))

has no list in it. The confusion is that we (English speakers) "list" things by separating them using commas. From Perl's perspective, there is nothing in that statement to provide a list context. "scalar" can take any expression, and just sees comma separators, no matter how many sets of parentheses you surround it with.

scalar(()=(1,3,5))

on the other hand, has an assignment that forces a list context, so the answer becomes "3".

if( (\$k, \$v) = ( 1, 0 ) )

works the same way. The left side of the assignment is now a list, because of the equals sign. The overall context of any boolean test is scalar, so that an "if list" always provides the element count.

@TGI: You have said that there is no list at all in:

```if( (5,3,0) )
I tested below. You are right.
```if( (5,3,0) ) is equal to if( 5,3,0 )
both evaluate to FALSE. I need to understand the definition of list furthur.

From your patient answers, I think I can draw the conclusion: The reason that list assignment in scalar context will return the number of elements on right side list is for convenience of usage.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003051]
Approved by frozenwithjoy
Front-paged by tye
help
Chatterbox?
 [Discipulus]: even with odd number of \ [Eily]: well, it allows you to concatenate strings to make a path without knowing if the final \ is already there or not [Discipulus]: also cd c:/ulisse\\\\\ strawberry \\..\\\\\\ strawberry & pwd runs fine

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-05-24 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (183 votes). Check out past polls.