http://www.perlmonks.org?node_id=130864


in reply to What is the difference between a list and an array?

Arrays and hashes can both be used as lists, witness:
my %F = (a=>1, b=>2, c=>3); my @F = qw(a 1 b 2 c 3); foo(%F); foo(@F);
#New simpler code to avoid slices my %F = (a=>1); my @F = qw(a 1); foo(%F); foo(@F);
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 ;-)

--
perl -p -e "s/(?:\w);([st])/'\$1/mg"

Replies are listed 'Best First'.
Re(2): What is the difference between a list and an array?
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 3

    On 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:

    my_sub( @a ); # ... sub my_sub { # here the localized @_ variable contains references to # the list of values passed as paramters, for my $i (0 .. $#_) { print "param $i = $_[$i]\n"; } } # OUTPUT # param 0 = a # param 1 = b # param 2 = c

    (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:

    @a = ( 'I', 'am' ); %h = ( 'perl' => 'hacker' ); $x = 'just'; my_sub( @a, $x, 'another', %h ); # OUTPUT: # param 0 = I # param 1 = am # param 2 = just # param 3 = another # param 4 = perl # param 5 = hacker

    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