Home » Perl Basics
4

References

References are scalar values and we can have an array or references.

Perl is a very advanced computer language, however with only the 3 data types at its disposal it is not as potent. The 3 data types are fairly unsophisticated, i.e. an array of an array cannot be created because arrays and hash can only contain scalar values. References being scalar values we can have an array or references. In effect, we are now capable of creating an array of hash, an array of an array ..etc.
Example:
You want to create a hash with the following values.

toyota , prius
Toyota ,camry
Toyota ,toundra
honda, civic
Honda , acura
Honda ,MDX
ford , escape
ford ,edge
ford ,focus

The most logical manner to arrange that data is a hash with the maker as a key and the list of model as values. Unfortunately HASHes only accept scalar as values. You cannot have a list. In order to go around that problem you need to use references.
References being a scalar you can include it into the hash it doesn’t matter to Perl that the reference point to a list.
Syntax :

Rule 1

  $name-of-your-reference = $scalar-variable
$name-of-your-reference = @array-variable
$name-of-your-reference = %hash-variable

 

Rule 2

You can also create a direct reference to an array or a hash without going through the creation of a regular array of hash before hand. This is called an anonymous hash or array as only the reference points to them.

$name-of-your-reference = [1 , 1, 3 ,45, 6,];  #anonymous array
$name-of-your-reference = { key1 => value1 , key2 => value2 , key3 => value3 }; #anonymous hash

IMPORTANT: While creating a non-anonymous array or hash, Perl relies on the variable name prefix to determine what kind of variable it is. "@" or "%" relies on [] or {} for anonymous ones. If you use () it will not work.

Examples


Example hash of arrays:

  $model_toyota = ["prius" , "camry", "toundra"];
$model_honda = ["civic","acura","mdx"];
$model_ford = ["escape","edge","focus"];
%m_m = ( "ford" => $model_ford ,
"honda" => $model_honda,
"toyota" => $model_toyota) ;
print "$m_m{toyota}[2]";
In that case the script will return “toundra”.
In that case we could also have used the anonymous list directly in the hash declaration

 

  %m_m = ( "ford" => ["escape","edge","focus"],
"honda" => ["civic","acura","mdx"],
"toyota" => ["prius" , "camry", "toundra"] ) ;
print "$m_m{toyota}[2]";
The return would have been the same. However if it is easily done for small script in bigger ones that kind of notation can become very difficult to read very quickly.

 

Example array of Hashes:

  $model_toyota = {"model1" => prius ,”model2”=> "camry", ”model3” => "toundra"};
$model_honda = {"model1" => "civic",”model2”=> "acura",”model3” => "mdx"};
$model_ford = {"model1" => "escape",”model2”=> "edge",”model3” => "focus"};
@m_m = ($model_ford , $model_honda, $model_toyota ) ;
print "$m_m[2]{model2}";

Example hash of hashes:

  $model_toyota = {"model1" => prius ,”model2”=> "camry", ”model3” => "toundra"};
$model_honda = {"model1" => "civic",”model2”=> "acura",”model3” => "mdx"};
$model_ford = {"model1" => "escape",”model2”=> "edge",”model3” => "focus"};
%m_m = ( "ford" => $model_ford ,
"honda" => $model_honda,
"toyota" => $model_toyota,);
print "$m_m{toyota}{model1}";

Example array of arrays:

  $model_toyota = ["prius" , "camry", "toundra"];
$model_honda = ["civic","acura","mdx"];
$model_ford = ["escape","edge","focus"];
@m_m = ($model_ford , $model_honda, $model_toyota ) ;
print "$m_m[2][2]";