|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: What is the difference between a list and an array?
by tye (Sage) on Dec 11, 2001 at 10:42 UTC | |
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") | [reply] [Watch: Dir/Any] [d/l] [select] |
(crazyinsomniac) Re: What is the difference between a list and an array?
by crazyinsomniac (Prior) on Dec 11, 2001 at 10:49 UTC | |
Consider this: Furthermore, the distinction between a list and an array goes to context. There are two context's in perl, list, and scalar. The scalar function forces scalar context. The for (@something) "construct" implies list construct, as do functions and other things. To get a better grip on context in perl, please read japhys excellent article from perlmonth.com Issue 9: "List" is a Four-Letter Word (local) (remote). | [reply] [Watch: Dir/Any] [d/l] |
by dmmiller2k (Chaplain) on Dec 12, 2001 at 00:38 UTC | |
A list is, well, just that, a list, a bunch of values separated by commas. You may be failing to address the confusion between what a list is with its literal representation in a program. After all a "bunch of values separated by commas" is considered a list literal, where the commas are mere notational conveniences. To the program itself, a list is a series of values, nevermind how they got there. This is NOT a flame! It goes more to highlighting the difference between what a thing is (e.g., list), and what you have to write in a program to get one. This particular distinction (or more precisely, the failure to understand it) caused more problems for me early on than maybe anything else (one exception: learning that my means local--in the C/C++ sense of the word--and that local really means localized global, or having a temporary local value for one But I digress). Fortunately for me, Perl's forgiving nature (combined with its uncanny "do what I mean" quality) saved my skin more than once. But when I later went back to examine some of my old code, I could see evidence of my then-incomplete understanding of these concepts. dmm You can give a man a fish and feed him for a day ...Or, you can teach him to fish and feed him for a lifetime | [reply] [Watch: Dir/Any] [d/l] [select] |
by dmmiller2k (Chaplain) on Dec 13, 2001 at 00:02 UTC | |
Truly, I meant no harm, in "Re(2) (crazyinsomniac): What is the difference between a list and an array?," above. Please consider it an (apparently unsuccessful) attempt at constructive criticism. I was hoping to articulate how your explanation might have perhaps obfuscated more than illuminated the subject for me, given my own understanding (or lack thereof) back when I was struggling with this concept myself, and in so doing, perhaps assist sierrathedog04 (et al) in that endeavor. dmm You can give a man a fish and feed him for a day ...Or, you can teach him to fish and feed him for a lifetime | [reply] [Watch: Dir/Any] |
Re: What is the difference between a list and an array?
by japhy (Canon) on Dec 11, 2001 at 09:11 UTC | |
Hashes are special -- there's no real non-hash hash-like structure in Perl that I can think of.
_____________________________________________________ | [reply] [Watch: Dir/Any] [d/l] |
Re: What is the difference between a list and an array?
by mischief (Hermit) on Dec 11, 2001 at 19:24 UTC | |
There is a good explanation in perlfaq4: What is the difference between a list and an array?
| [reply] [Watch: Dir/Any] [d/l] |
Re: What is the difference between a list and an array?
by belg4mit (Prior) on Dec 11, 2001 at 09:09 UTC | |
Within each code block both calls to foo pass the same values; @_(the parameters list) will be the same. NOTE: It is luck that the first works, as it relies upon the particular key/value pairs and how the current implementation of hashes in perl works. Basically an array is an actual thing that lives somewhere and has an address (you can get a glob for it, you can use a reference to it), but that is not so for a list. UPDATE: Modified code to ensure hash order prompted by dmmiller2k's reply. Which of course makes the comparison a little less clear since it uses slices. Added a second code block to reclarify UPDATE I am an idiot. The sliced version only returned the values. And it did in fact work as advertised to begin with. It was just lucky that it did so. Added the NOTE, I really do understand this, but apparently I'm not very good at explaining it with code ;-)
-- | [reply] [Watch: Dir/Any] [d/l] [select] |
by dmmiller2k (Chaplain) on Dec 11, 2001 at 23:51 UTC | |
Just to add my voice to this cacophany ... If I recall my own confusion on this point correctly, and the explanation that helped me finally turn the corner (toward understanding), a "list" may be described as a context, or a way of interpreting "thingys" (or variables), rather than as a thingy in its own right, whereas an array is one of three types of containers (thingys) your program may use to hold other thingys (information). For example, an array, <CODE<>@a</CODE>, containing the three values, 'a', 'b' and 'c' (or @a = ('a', 'b', 'c')); in scalar context (in other words when the context requires a single value), it will evaluate as 3 (its number of elements). An example of this is a simple assignment to a scalar: $x = @a; # will contain 3On the other hand, in list context (or, when the context permits multiple scalar values), the array will be flattened into a series of values of its elements, as in the classic case, when it is passed as a parameter to a (non-prototyped1) sub:
(Hashes may also be similarly flattened into lists, where the key/value pairs are guaranteed to remain adjacent and in 'key', 'value' order, but the ordering of the pairs is not necessarily deterministic.) If array thingys and hash thingys and scalars are all intermingled in a given list context, they are all flattened into a single list of all the individual elements:
1 Prototypes can alter this behavior, but that's a whole 'nother conversation. dmm You can give a man a fish and feed him for a day ...Or, you can teach him to fish and feed him for a lifetime | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: What is the difference between a list and an array?
by Hanamaki (Chaplain) on Dec 11, 2001 at 21:14 UTC | |
Hanamaki | [reply] [Watch: Dir/Any] |
Re: What is the difference between a list and an array?
by fmogavero (Monk) on Dec 11, 2001 at 23:55 UTC | |
Washington, Lincoln, Jefferson is a list. @streets = (Washington, Lincoln, Jefferson) or @presidents = (Washington, Lincoln, Jefferson) make the list an array. I've always thought of lists as an unnamed group of items whereas an array is a named group of items. Does someone have any other elaborations for this? | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: What is the difference between a list and an array?
by tradez (Pilgrim) on Dec 12, 2001 at 00:19 UTC | |
I get the number of elemets in the array. However if I do I get the first value of the list popped into this scalar value. | [reply] [Watch: Dir/Any] [d/l] [select] |
by chromatic (Archbishop) on Dec 12, 2001 at 01:05 UTC | |
| [reply] [Watch: Dir/Any] [d/l] |
by Anonymous Monk on Dec 13, 2010 at 06:01 UTC | |
| [reply] [Watch: Dir/Any] |