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:
- a "list literal" might be used to mean "a list of scalar-value expressions separated by commas and surrounded by parens"
- a list of SVs arranged together in memory, such as on the stack
- "an operation that would return a list of scalar values if used in a list context"
- the argument list passed to a subroutine
- I don't yet have a good label for what goes between the parens in a foreach statement -- it is naively a "list of scalar values" but is functionally much more like an "argument list" but calling it the latter would confuse people (stolen form (tye)Re3: tr doesn't *really* use a list)
(tye)Re3: List context or not? has some more of my thoughts on "lists" in Perl.
But there are a few things that make an array different from a listlike thing that isn't an array:
- The @ character can be used to introduce an array (if you can't use @ with what you have, then you don't have an array)
- This means that you can use an array with push, pop, shift, unshift, and splice.
- You can get a reference to an entire array. You can't get a reference to an entire non-array "list" except using [ ... ] which is cheating because it makes a new array, copies the "list" into it, and then gives you back a reference to the new array.
You see, an array is like a list of scalar values with an extra chunk of data that keeps track of bookkeeping information about where the list is, how much space is allocated for the list, etc. This means that you can add items to the list of values stored in an array because the that extra chunk of data allows the array to reallocate space to make more room. Then everyone who is using that array knows where the list of values had to be moved to when more room was required.
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")
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.