Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl array vs list

I have two data structures in Perl:

An array:

my @array2 = ( "1", "2", "3");  for $elem (@array2) {     print $elem."\n"; } 

Giving me the following output:

1 2 3 

And a list:

my @array = [ "1", "2", "3"];                                              for $elem (@array) {     print $elem."\n"; } 

Giving the following output:

ARRAY(0x9c90818) 

Obviously, I'd like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array?

like image 863
Marcin Cylke Avatar asked May 16 '11 21:05

Marcin Cylke


People also ask

What is the difference between Perl list and Perl array?

In Perl, List and Array terms are often used as if they're interchangeable. But the list is the data, and the array is the variable.

Is array or list faster?

The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.

What is the use of array in Perl?

In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable.

What are lists in Perl?

A list in Perl is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list.


1 Answers

Lists in Perl are not data structures, they are positions in the source code, determined by the context around them. Lists are basically the transient structures that Perl uses to move data around. You interact with them with all of Perl's syntax, but you can not work with them as a data type. The data type that is closest to a list is an array.

my @var    =    (1, 2, 3);  # parens needed for precedence, they do not create a list    ^ an array    ^ a list  say 1, 2, 3;     ^ a list  say @var;     ^ a list (of one array, which will expand into 3 values before `say` is called) 

When you write [1, 2, 3] what you are doing is creating a scalar array reference. That array reference is initialized with the list 1, 2, 3, and it is the same as creating a named array and taking a reference to it:

[1, 2, 3]   ~~   do {my @x = (1, 2, 3); \@x} 

Since the [...] construct creates a scalar, you should hold it in a scalar:

my $array = [1, 2, 3];                                              for my $elem (@$array) {   # lexical loop variable     print $elem."\n"; } 

Since you want to operate on the whole array, and not just the reference, you place a @ in front of the $array which dereferences the stored array reference.

like image 145
Eric Strom Avatar answered Sep 30 '22 07:09

Eric Strom