We don't bite newbies here... much PerlMonks

### Re: List context or not?

 on Mar 13, 2001 at 01:04 UTC ( #63954=note: print w/replies, xml ) Need Help??

in reply to List context or not?

I think you may be confusing a list with an array. An array does return the number of elements when used in a scalar context. But a list does not. A list returns the last item in a list. Thus:
$a = (10, 20, 30); #$a = 30

@nums = (10, 20, 30);
$a = @array; #$a = 3

[download]
And I think your subroutine is returning a list, not an array.

Replies are listed 'Best First'.
(tye)Re2: List context or not?
by tye (Sage) on Mar 13, 2001 at 02:12 UTC

when used in a scalar context. A list returns the last item in a list.

Lots of different types of lists return lots of different things in scalar contexts. The most common behavior is to return the last item.

- tye (but my friends call me "Tye")
I'm not aware of different types of lists having varying behavior in scalar context. Are there some examples?

My original response was based on the documentation in perldata - "If you evaluate a named array in a scalar context, it returns the length of an array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.)"

Depends what you mean by "list". The problem is that most people have some rather specific definition of "list" in mind which often doesn't match some other definition of "list" that the person they are communicating with is using.

I suspect that the author of the documentation that you quoted was thinking specifically of a "list literal", that is, some parens around a bunch of scalar value expressions that are separated by commas, or thereabouts.

You appear to have silently extended this definition to include "list slices". Well, "list slices" happen to also return their last entry in a scalar context. But if you are going to include "list slices" in your definition of "list", then I don't see how you can exclude lots of other things from your list of "lists". For example, is a list assignment a list? Is an array a list? How about a hash? They are all lists to me (and are even talked about as lists in the standard documentation).

But there are a ton of things that people will think are "lists" and even that are called "lists" (even if we restrict ourselves just to the standard documentation that comes with Perl).

Actually, I try not to refer to anything as a "list" because of this false assumption that is common among Perl programmers that they know what a "Perl list" is. (: For the above things, I call them "operations that would return a list of scalar values if used in a list context". Note that that definition includes a lot of built-in functions (and non-build-in functions) but also covers lots of things that the quoted documentation seems to have overlooked.

Even if I stick to "list literals", I find the broad statement that you quoted to be misleading. In a list context ("a","b",@c) returns a list of scalar values. In a scalar context it doesn't return the last element of that list of values. It returns the last "element" of the "list literal" interpretted in a scalar context, that is, scalar(@c). I don't think "a list in a scalar context returns its last value" catches that idea very clearly.

In fact, if we look at the bit of documentation more closely in regard to my example, they must be referring to @a as a "value". Pretty strange terminology and, to me, it is even more indication that the author of the section was not being careful enough.

Apparently this is rant topic for the day. Tomorrow, "Whether or not to call srand() and how". (:

- tye (but my friends call me "Tye")
Re: Re: List context or not?
by sierrathedog04 (Hermit) on Mar 13, 2001 at 02:10 UTC
I have apparently been confused for quite some time. I used to think that a list and an array were the same. The Camel book says, however, that
@stuff = ("one", "two", "three");
[download]
assigns the entire list value to array @stuff, but:
$stuff = ("one", "two", "three"); [download] assigns only the value three to variable$stuff.

So contrary to what I had thought a list is not an array. It is sort of a proto-array, a thingy that one might likely turn into an array but is not there yet.

There is also something called a reference to an anonymous array. One creates it using brackets as in:

\$arrayref = ["one", "two", "three"];
[download]
Since lists are comma-separated lists of values the text inside the brackets above is an example of an anonymous array which is also a list. However, lists are not in general arrays, they have to be assigned to arrays.

Create A New User
Node Status?
node history
Node Type: note [id://63954]
help
Chatterbox?
 erix . o O( "what fools the french are, Jeeves" ) [choroba]: Also some Достое́вс кий [Discipulus]: mmh windows understand something like: cd c:\\\\path\\\\to weird..

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

Results (183 votes). Check out past polls.