In the previous lesson you learned about Perl's capacity to create and use references to data. Specifically, you learned about scalar references.In this lesson we will cover references to arrays. References to arrays are just like references to scalars, where the only difference is that the reference knows it points to an array. You can create a reference to an array like this:
@presidents = qw( Franklin Harry Dwight
John Lyndon Richard Gerald Jimmy Ronald George Bill );
$arrayref = \@presidents;
This will create a reference that points to the array.
What are references to Arrays in Perl?
In Perl, a reference to an array is a scalar value that points to an array rather than storing the array directly.
It allows you to create more complex data structures and manipulate arrays dynamically. A reference to an array can be created using the backslash operator \@ before the name of the array:
my @array = (1, 2, 3);
my $array_ref = \@array;
Once you have a reference to an array, you can access its elements by using the arrow operator -> and square brackets []:
print $array_ref->[0]; # prints 1
print $array_ref->[1]; # prints 2
Referencing arrays in this way allows you to pass arrays to subroutines, return arrays from subroutines, store arrays in hashes, and build complex data structures like arrays of arrays.
You can get at the values in the array like this:
$$arrayref[1] # an element of the dereferenced array
$arrayref # the whole dereferenced array
Optionally, you can dereference an array with the special arrow operator:
$arrayref->[1] # an element of the array
You can also create anonymous arrays that are accessible only via reference. This may seem pointless now, but we will use it quite a bit later. So take a minute now to work through an example of a Anonymous Array.
Perl Anonymous Arrays
You create an anonymous array by using square brackets like this:
$arrayref = [ 'one', 'two', 'three' ];
There exists an array that has no name but is available via the $arrayref reference, which is just like any other array reference in all other respects. You can easily create a two-dimensional array as follows.
That makes a reference to an anonymous array ($arrayref), which contains three scalars ('one', 'two', and 'four') and another anonymous array ([ 1, 2, 3 ]). You can dereference the values in the array like this:
$arrayref->[1] # 'two'
$arrayref->[2][1] # 2
You can even expand an anonymous array without any arbitrary restrictions.
So far, we have created references to previously existing variables. Now we will learn to create references
to "anonymous" data structures which are values that are not associated with a variable.
To create an anonymous array, use square brackets instead of parentheses:
$ra = [ ]; # line 1
$ra = [1,"hello"]; # line2
Creates empty, anonymous array, returns a reference to it
Creates an initialized anonymous array, returns a reference to it
This notation not only allocates anonymous storage, it also returns a reference to it, much as malloc(3) returns a pointer in C.
Question: What happens if you use parentheses instead of square brackets?
Recall again that Perl evaluates the right side as a comma-separated expression and returns the value of the last element;
$ra contains the value "hello", which is likely not what you are looking for.
Use cases for Perl anonymous arrays
Perl anonymous arrays are arrays that are created without assigning them to a named variable. They are useful in various scenarios where you need to create a temporary array, pass an array to a function, or store arrays within data structures like hashes or other arrays. Here are several use cases when you might use Perl anonymous arrays:
Temporary arrays: When you need a temporary array for a short-lived operation, anonymous arrays can be useful. By not assigning a name to the array, you can create a concise and efficient representation.
print join(", ", @{[1, 2, 3, 4, 5]}), "\n";
Passing arrays to functions: Anonymous arrays can be used to pass an array to a function without creating a named array beforehand. This can make your code more concise and easier to read.
sub print_array {
my ($array_ref) = @_;
print join(", ", @$array_ref), "\n";
}
print_array([1, 2, 3, 4, 5]);
Storing arrays within arrays (multidimensional arrays):
Anonymous arrays can be used to create multidimensional arrays by nesting them within other arrays. This allows you to represent more complex data structures.
my $matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Storing arrays within hashes:
When you want to associate arrays with keys in a hash, anonymous arrays can be used to create and store the arrays directly within the hash.
Returning arrays from functions: Anonymous arrays can be used to return arrays from functions without having to create named arrays within the function.
sub create_array {
my ($start, $end) = @_;
return [($start .. $end)];
}
my $array_ref = create_array(1, 5);
print join(", ", @$array_ref), "\n";
These use cases demonstrate the versatility and convenience of Perl anonymous arrays in various situations, such as passing arrays to functions, creating temporary arrays, and storing arrays within data structures like hashes and other arrays.