3

I read about scalars arrays and list. I am not sure what is meant by list. For example, (5, apple, $x, 3.14) is a list, but what is the variable actually referencing the list?

Are lists just a way to initialize arrays or is it the known data structure?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • 3
    See this answer: http://stackoverflow.com/questions/6023821/perl-array-vs-list – squiguy Apr 10 '13 at 18:14
  • 1
    @Wooble, that is not correct. Arrays and lists are two distinct entities in Perl. Take a look at the top answer of the question referenced by squiguy. – Matthias Apr 10 '13 at 18:21

3 Answers3

3

How do you reference a list in Perl?

The same as you do with anything else - with the reference operator \. See perlreftut and perlref.

my $x;
\(5, 'apple', $x, 3.14)

The expression returns

(
    \ 5,
    \ "apple",
    \ undef,
    \ 3.14
)

which is not that useful.

For example, (5, apple, $x, 3.14) is a list, but what is the variable actually referencing the list?

There is none. It is an anonymous value.

Not every value needs a variable to hold it - literals are also an important part of programs.

Are lists just a way to initialize arrays or is it the known data structure?

Read Perl array vs list.

Community
  • 1
  • 1
daxim
  • 39,270
  • 4
  • 65
  • 132
3

There are three different basic data structures in Perl.

  • Hashes: These are key/value pairs.
  • Arrays: These are ordered values.
  • Scalars: This is a single value.

Hashes:

my %employee = (
    "A001" => "Albert",
    "A002" => "Bob",
    "B003" => "Gary",
);

print "$employee{A001}\n";   #Prints "Albert"
print "$employee{B003}\n";   #Prints "Gary"

Arrays:

my @fruit = ("Apple", "Orange", "Banana");
print "$fruit[0]\n";   #Prints "Apple"
print "$fruit[2]\n";   #Prints "Banana"

Scalars:

my $age = "None of your business";
print "You're $age years old\n";  #Prints "You're None of your business years old

A List is merely a list of items. For example, @fruit is an array. However, I set my array to equal a list containing the names of fruits.

Technically, a list is unchangeable while an array is a data structure that can be modified.

for my $color ("red", "green", "blue") {
    print "$color is an item from my list\n";
}

In the above, my for loop advances through a list:

my @list = ("red", "green", "blue");
for my $color (@list) {
   print "$color is a member of my array\n";
}

The above is pretty much the same code, but now my for loop advances through an array.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David W.
  • 105,218
  • 39
  • 216
  • 337
  • So what is this: `($a)=@foo`? How do I use the list? – Cratylus Apr 10 '13 at 19:44
  • @Cratylus This is similar to `($a, $b, $c, $d) = (1, 2, 3, 4);`. This is assigning 1 to `$a`, 2 to `$b`, etc., or `($a, $b, $c, $d) = @a` if `@a` is equal to `(1, 2, 3, 4)`. If there are fewer variables on the left than members of the list/array on the right, the other members of the list/array are not assigned to a variable. In the end, `($a) = @foo` is the same as `$a = $foo[0];`. – David W. Apr 11 '13 at 00:39
0

On a really basic level I think understanding what an operator is helps.

This can be summed up by saying anything which is not a value, is an operator.

What is the list operator?

,

How does the list operator work?

The list operator evaluates the value on the left, then evaluates the value on the right. If the value on the right of itself also has a list operator, the associativity of the operator means the evaluations will continue until no more list operators appear or end of line.

#!usr/bin/perl
use warnings;
use strict;

my @namedlist = 1, my @namedlist2 = 2;

print @namedlist , @namedlist2;

12

Once you understand the list operator, you can then deal with the real issue of your problem:

context

For example, print supplies list context to its arguments. It expects a list, not a singular value, so it supplies the list context.

Now we know that the [], and {} operators, are the reference to an anonymous array constructor, and reference to anonymous hash constructor respectively, but I think few realise that before these there was only the list constructor operator. The list constructor operator is

()

Why does this matter? Well, because the list operator , does not know about context. It just evaluates and discards, evaluates and discards.

Supply the context by wrapping a list operator with a list constructor (), and now the list constructor captures the evaluations of the list operator so that it can return a list.

Of course you will be using the assignment operator = to assign the newly constructed list returned by the list constructor (), to a named list holder. Such as an array or a hash.

my @list = (one, 1, two, 2);
my %associatedlist = (three, 3, four, 4);

So a list is a series of evaluated values captured by the list constructor, and then usually assigned into a named list holder.

This is apparent through the reverse engineering of the reference to list constructors, [] and {}. There being a difference between the two reference to list constructors, highlights their use as operators, perhaps descending from earlier usage of a similar operator?

The reference to list constructers, must be explicitly stated through the use of either the reference to the anonymous array constructor [], or reference to the anonymous hash constructor {}.

The list constructer () is constructing a list of values which are then generally assigned to either a named array or named associated array. Though the assignment operator = does not distinguish between its leftvalue and rightvalue, it just attempts to assign the right to the left, if the contexts are incompatible or the list sizes are of differing lengths, it then proceeds to compliment your handiwork to STDOUT accordingly.

useless use of constant in void context at roughly 1 foot from keyboard.pl
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
doncoyote
  • 168
  • 7