Array reference
A data structure called array reference frequently appears in Perl. This page details array reference from References.
What is an array reference?
An array reference is a reference to array. A reference means to point. You can use the reference generator "\" on an array to create an array reference.
# Create array reference my @nums = (1, 3, 5); my $nums_ref = \@nums;
Note that the array reference is assigned to a scalar variable.
Let's write an image of the array reference below.
# image @nums $nums_ref - -- -->(1, 3, 5)
An array reference represents "what points to an array" rather than the array itself.
Array reference is assigned and the contents are not copied
Let's look at the difference between arrays and array reference from here.
The entire contents of the array are copied by assignment, but the array reference is not copied.
# Arrays are copied with values my @nums1 = (1, 3, 5); my @nums2 = @nums1; # Array reference does not copy values my @nums1 = (1, 3, 5); my $nums_ref1 = \@nums1; my $nums_ref2 = $nums_ref1;
Below is an image. In the case of an array, the entire contents are copied, whereas in the array reference, the reference itself is copied, not the contents.
# image @nums1 @nums2 (1, 3, 5) (1, 3, 5) $nums_ref1 - -- -- -- ->$nums_ref2 - -- -- -- ->(1, 3, 5)
When passing to subroutine, it is more efficient to pass an array reference because the contents are not copied.
# Pass an array reference to a subroutine my_func ($nums_ref1);
Anonymous array generator
Array reference can be created using the reference generator "\", but this is a bit tricky. An operator called anonymous array generator is defined to quickly create an array reference.
my $nums_ref = [1, 3, 5];
Note that regular arrays are created with "()", whereas array reference are created with the anonymous array generator "[]".
This notation appears frequently in Perl source code. It is important to read from the source code whether it is an array or an array reference.
# arrangement my @nums = (1, 3, 5); # Array reference my $nums = [1, 3, 5];
Dereference
I used a reference generator to generate an array reference. Now let's extract the array from the array reference, on the contrary.
To do this, perform an operation called dereference .
my $nums_ref = [1, 3, 5]; # Dereference my @nums = @{$nums_ref};
Dereference can be made using the notation "@{array reference}".
If the array reference is a scalar variable, you can omit the "{}".
my @nums = @$nums_ref;
Pass as an argument to a builtin functions
shift function, unshift function, pop function and push take an array as an argument. So be aware that if you want to pass an array reference, you need to dereference it.
# Dereference and pass to builtin functions shift @$array; unshift @$array, $element; push @$array, $element; pop @$array;
Extract array values from array reference
You can use the arrow operator "->" to retrieve an array value from an array reference.
# Get array values from array reference my $num = $nums_ref->[3]; # Set array values from array reference $nums_ref->[3] = 67;
If the arrow operator is used, it is an array reference. If not, it's an array. Please be able to read this well in the source code.
# Get array values my $num = $nums[3]; # Set array values $nums[3] = 67;
It's important to look closely, as it depends on whether it's an array or an array reference, with or without the arrow operator.
Hash reference
Hash reference are as often used as array reference.
my $score_ref = {math => 78, english => 89};
Hash reference are explained in detail below.