in reply to What is the difference between a list and an array?
I think of an array as a variable that can hold a list of scalar values. The term "list" can mean any number of things in Perl. I suggest you reread a thread you participated in a long time ago: Scalars, Lists, and Arrays.
Many of the meanings of "list" in Perl come close to "a list of scalar values", but that still doesn't nail things down very specifically. You can have:
But there are a few things that make an array different from a listlike thing that isn't an array:
If you want to add values to a list that isn't an array, then you really end up making a new larger list that contains the values (or copies of the values) from the original list.
A hash is like an array. You could think of it as a container for a list of pairs (where each pair is one string and one scalar value). If you take the contents of a hash out of the hash, you have a "list" (of scalar values, every other one of which must be just a string).
Also note that Perl gives you a way to find specific elements of a list (the "list slice" -- (list)[1,2]) but there is no way to "use a list as a hash" other than creating a (perhaps temporary) hash.
Also, a "list" is a rather temporary thing. If you want to find out how many things are in a list, you can do: my $size= ()= LIST; but now you've lost the "list" and so you'll either have to recreate or keep a copy of it. And about the only way to keep a copy of it is to stuff it into an array. Similarly, if you want the last item of a list, you can use my $last= (LIST)[-1]; but you had better not have wanted to know anything else about the list as you've now lost it again.
Sure, you can get several items out of the list at once using a list slice. But you can't get several items out of the list and also record the size of the list unless you put the list into an array at least temporarilly. And you can't do a lot of other things like use the first element of the list as the index of which element of the list you want to grab. Even passing a list into a function causes Perl to place that list into an array (@_) for you. (:
- tye (but my friends call me "Tye")
|
|---|