Broad Network


Perl Reference to an Array

Perl References - Part 2

Forward: In this part of the series, we look at Perl Reference to an Array.

By: Chrysanthus Date Published: 12 Aug 2012

Introduction

This is part 2 of my series, Perl References. You should have read the previous part of the series before reading this one. The titles of the different parts of the series are given at the bottom of this article. In this part of the series, we look at Perl Reference to an Array.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

An Array Reference
A reference to a scalar or array or hash is a short piece of code (address) that refers to the scalar or array or hash respectively. A reference is held by a scalar variable independent of whether the value is a scalar, or array or hash. There are two common ways to create a reference to an array.

Creating a Reference with Backslash
The following code segment shows how to create a reference to an array with a backslash:

    @arr = ("one", "two", 3, 4);
    $aref = \@arr;

The first statement creates an array in the normal way. In the second statement the right operand is the array variable preceded by the backslash. The resulting right operand, which is a reference to an array, is assigned to a scalar variable. So the reference to an array is held by a scalar variable and not an array variable. A reference is just a short piece of code (address) that refers to a region in memory; an array with its elements occupy a region in memory. When dealing with a reference, you are more interested in the region occupied by a value (array) in memory than the variable that identifies the value.

Reference from Anonymous Array
Anonymous means, no name. The following code shows how to create a reference from an anonymous array:

    $aref = ["one", "two", 3, 4];

This array is the same array as the one above, but it has no name. It would occupy the same memory region as the one above. However, here, instead of arc brackets, you have square brackets. The square brackets return a reference to the array (region). This reference, which would be the same as the one above (@arr) is assigned to the scalar variable, $aref. The reference to a scalar, array or hash is held by a scalar variable.

In the previous reference situation, the name of the array is arr; however, here the array has no name. That is why you have one statement here and not two in order to create the reference.

Obtaining an Array from a Reference
One way to get the array from an array reference (scalar variable holding the reference) is to use the braces. For the above reference, you would type,

    @{$aref}

You begin with the array sign, @, since you are dealing with an array. This is followed by braces. Inside the braces, you have the scalar variable that holds the reference.

You usually do not use the array as a whole (as indicated above). You usually use an element from the array. For an array that has a name, if you want to use the array name to get an element, you would type something like,

    $arr[2]

where the name of the array (variable) is arr. When you have a reference to the array, you do a similar thing but with the braces as follows:

    ${$aref}[2]

That is, you replace, arr with “{$aref}”.

Another way of accessing an array is also applicable when you want an element from the array (this is what you do most of the times). With this way, you do not start with the preceding scalar sign, $. You also omit the braces. However, you follow the array reference variable, with an arrow, -> (minus sign followed by greater than sign), as in the following example:

    $aref->[2]

Now, @{$aref} can be abbreviated to @$aref without the braces, and similarly ${$aref}[2] can be abbreviated to $$aref[2]. However, $aref->[2] cannot be abbreviated by omitting ->, as $aref is holding a reference and aref is not the name of an array.

Read and try the following code, which illustrates the use of reference to an array:

use strict;

print "Content-Type: text/html\n\n";

print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";

my @arr = ("one", "two", 3, 4);
my $aref = \@arr;
print $$aref[1] . "<br />";

my $arref = ["one", "two", 3, 4];
print $arref->[3];

print "</body>\n";
print "</html>\n";

That is it for Perl Reference to an Array. We stop here and continue in the next part of the series.

Chrys

Related Links

Perl Reference
Object Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message