Broad Network


Reference to an Array in Perl

Perl References Optimized Part 2

Perl Course

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

By: Chrysanthus Date Published: 10 Jul 2015

Introduction

This is part 2 of my series, Perl References Optimized. In this part of the series, we look at Perl Reference to an Array. You should have read the previous part of the series before reaching here, as this is a continuation.

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;

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

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

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 Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message