manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

whats the difference between array and list in perl ?
This is what I found on http://www.tutorialspoint.com/perl/perl_arrays.htm
"Lists are really a special type of array - .essentially, , a list is a temporary construct that holds a series of values. The list can be "hand" generated using parentheses and the comma operator, "
I dont understand what this means. Please clarify this statement and please point to obvious difference between array and list.
Is it like when an array is created without any double quotes or single quotes, its called list ?
@list1 = (1,2,3,4); # example of list
@list2 = (list1, list2, list3, list4); #example of list
@arr1 = ("1" , "2", "3", "4"); # example of array
@arr2 = ("arr1", "arr2", "arr3", "arr4") ; # example of array

Is this understanding correct ?
I could not find much information about it online. When shall we prefer to use list over array or array over list ?
  • Comment on difference between array and list in perl

Replies are listed 'Best First'.
Re: difference between array and list in perl
by toolic (Bishop) on Jan 16, 2010 at 18:13 UTC
Re: difference between array and list in perl
by LanX (Saint) on Jan 16, 2010 at 18:49 UTC
    A lot of theories are plain wrong, even the docs are not consistent...(e.g. naming "wantarray" instead of "wantlist")

    An "Array" is a variable with allocated space in memory (noted/accessed with @x or $x[...] or [a,b,c] resp.) a "List" is either only syntax in the source, i.e. comma seperated expressions² or the mechanism for passing multiple values (e.g. when calling and returning from a sub¹) !

    So your example

    @list2 = (list1, list2, list3, list4); #example of list (WRONG!)

    Shows a list at RHS whose values are assigned to an array-variable LHS.

    Unfortunately in perl5 it's not simply possible to identify a list as a "literal array", because there are plenty of subtle differences, eg with @A=(a..c) these snippets $a=@A and $a=(a,b,c) produce totally different results.

    UPDATE: Technically there is nothing like a "literal array", even if you construct an anonymous array reference  [a,b,c] you have a literal list a,b,c used to initialize an array!

    You may want to search in the archives of the monastery ... it's really a f.a.q. ...

    Cheers Rolf

    1) The values are passed on a internal stack which exists only temporarily.

    2) The parens are only for grouping.

    UPDATES: different clarifications...

      "wantarray" is a misnamed feature, but it is documented accurately ("... looking for a list value").
        as I already said ...the docs are not consistent...(e.g. naming "wantarray"...
Re: difference between array and list in perl
by cdarke (Prior) on Jan 16, 2010 at 21:35 UTC
    Calling a list a speial type of array is a little misleading. An array is a variable, a list is not. In your example:
    @list1 = (1,2,3,4); @list2 = (list1, list2, list3, list4); @arr1 = ("1" , "2", "3", "4"); @arr2 = ("arr1", "arr2", "arr3", "arr4") ;
    @list1, @list2, @arr1, and @arr2, are all arrays. In all your examples the right hand side of the assignments are lists, although the list (list1, list2, list3, list4) is questionable, since it contains bare words which might be interpreteted as subroutine calls. To ensure text you can autoquote each word using qw, for example:
    my @arr3 = qw(value1 value2 value3 value4);
    qw returns a list (in an old release it used to return an array, and that was considered to be a bug).

    Fortunately you can assign the items in a list to an array, as you have. When we do this kind of assignment it is essentially a copy of the items on the right hand side into elements in the array.

    You can assign to a list on the left side, provided the list only consists of variables (although undef is a special case). For example:
    my @arr5 = qw (quick brown fox); my ($speed, $colour, $animal) = @arr5;
    will assign the three values to the array, then assign from the array into the three variables $speed, $colour, $animal arranged in a list.
      although the list (list1, list2, list3, list4) is questionable, since it contains bare words which might be interpreteted as subroutine calls
      Whether they are subroutine calls or not doesn't matter. It's a list because of the context - the RHS of a list assignment is always a list. The fact the assignment is a list assignment is caused by the LHS being an array.
      qw returns a list
      No, it doesn't. In Perl, it's not the operation (operator, subroutine, function) that determines whether a list or a scalar is returned - it's the context. It's always the context. qw in scalar context cannot return a list - there are no lists in scalar context.1
      in an old release it used to return an array
      In which release was that? And what does "returning an array" mean? Could I push on it? Slice from it? Did it have a name? Perhaps you're confused by the fact in earlier releases qw was calculated at run-time (by doing a split), and nowadays qw is calculated at compile time.
      You can assign to a list on the left side, provided the list only consists of variables....
      ...then assign from the array into the three variables $speed, $colour, $animal arranged in a list.
      You're contradiction yourself. First you say it assigns to a list, then you say it assigns to a three variables. It's actually the latter that happens.

      1Who will be the first one to claim this time there's such a thing as lists in scalar context?

        Who will be the first one to claim this time there's such a thing as lists in scalar context?

        There is. Here's one:

        >perl -MO=Concise -e"$x = (4,5,6)" 2>&1 | find "list" 5 <@> list sKP ->6 ^ ^ | | list scalar context

        That's why it's clearer to say "an expression cannot return a list in scalar context". In the above example, the list evaluates to the scalar 6.

        Since you know better, why do you use such a needlessly confusing sentence?

        > there are no lists in scalar context

        This is - as usual - a definition conflict!

        As I already pointed out there are (at least) two widespread interpretations of what "LIST" mean:

      • a literal comma separated sequence of expressions visible in source code
      • the realization for passing theses values at run time through an internal stack

        Take this example code:

         perl -e 'print scalar ($a++,"b","c"), $a'

        Following your explanation one might think that $a should never be incremented, since the "list" (the stack) is never built!

        But actually one gets c1 as output, since the "list" (the sequence) is evaluated step by step

        Cheers Rolf

        UPDATE: shortened post

Re: difference between array and list in perl
by matze77 (Friar) on Jan 16, 2010 at 18:10 UTC
    I think it is also important to remember when "list context" and "scalar context" is "used" This holds surprises for me every time i do it wrong again ;-)... http://perl.plover.com/context.html
Re: difference between array and list in perl
by ikegami (Patriarch) on Jan 17, 2010 at 07:36 UTC

    Lists are really a special type of array - .essentially, , a list is a temporary construct that holds a series of values. The list can be "hand" generated using parentheses and the comma operator,

    There are many definitions of list, so it's hard to describe what it is. Among other things, it can refer comma separated values in the source (a list literal), the operator produced by that code (a list operator) or the values the operator places on the stack (a list value).

    On the other hand, the definition of array is quite clear. First and foremost, it's a type of variable. Very few if any properties are shared between arrays and lists (whichever definition you use) since lists are never variables in Perl.

    Is it like when an array is created without any double quotes or single quotes, its called list ?

    An array is never called a list. An array is never a list. It may return one. A list may be assigned to one.

    In all of your examples, you are assigning a list to an array. You could also say you are initialising an array.

    arrays lists vvvvvv vvvvvvvvvv @list1 = (1,2,3,4); @list2 = (list1, list2, list3, list4); @arr1 = ("1" , "2", "3", "4"); @arr2 = ("arr1", "arr2", "arr3", "arr4"); ^ assignments
Re: difference between array and list in perl
by WizardOfUz (Friar) on Jan 16, 2010 at 18:00 UTC

    Arrays are ordered lists of scalars. Please take a look at perldata which explains these terms.

Re: difference between array and list in perl
by ivo_ac (Acolyte) on Jan 17, 2010 at 14:56 UTC
    In my understanding (and as explained by "Learning perl"), an array is a variable containing the list. So the list is the data itself, the array is the variable in which it is stored.
      Yes, you can say an array contains a list (of elements). I just wanted to warn against making the incorrect assumption that lists are only found in arrays.