So now you’re comfortable with Perl its time to move on to Extending those hashes and arrays.
Referencing – \
An important part of Perl is referencing. A reference is a “pointer” to the data in Perl’s eyes. References must be dereferenced to work.
To reference something in Perl add a backslash before an array or hash.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
use strict;
my @array = qw ( Socks Milly Poppy);
my $arrayref = \@array;
print $arrayref; #Will print something like ARRAY(HEX)
#ref, A useful function to check what a reference contains
print ref($arrayref); #Prints "ARRAY"
my %hash = { "Joe", "Socks", "Matt", "Poppy", "Pete", "Milly"};
my $hashref = \%hash;
print $hashref; #Prints HASH(HEX)
print ref($hashref); #Prints HASH
|
Dereferencing – {}
Now what good is having this data in a reference? To make it usable again you must dereference it.
Dereferencing depends on the data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
use strict;
use Data::Dumper;
#
# Strings
#
my $string = "Socks";
# Referenced
my $stringref = \$string;
# Dereferenced
my $newstring = ${$stringref};
print $newstring; #Prints "Socks"
#
# Arrays
#
my @array = qw ( Socks Milly Poppy);
# Referenced
# \@array
my $arrayref = \@array;
# Dereferenced
# @{$ref}
my @newarray = @{$arrayref};
print @newarray; #Prints Socks Milly Poppy
#You can also do
print ${$arrayref}[1]; #Prints Milly
#
# Hashes
#
my %hash = ( "Joe" => "Socks", "Matt" => "Poppy", "Pete" => "Milly");
# Reference
my $hashref = \%hash;
# Dereferenced
# %{$ref}
my %newhash = %{$hashref};
print %newhash; # Prints the hash
#Also possible:
print ${$hashref}{"Joe"}; #Prints Socks
|
To recap:
Strings
- Reference: \$string
- Dereference: ${$reference}
Arrays
- Reference: \@array
- Dereference: @{$reference}
- Access an element: ${$reference}[0]
Hashes
- Reference: \%hash
- Dereference: %{$reference}
- Access a value: ${$reference}{“key”}
Okay, now I expect you’re thoroughly confused. Why use references in the first place?
They can be manipulated very easily.
Array of Arrays
If you have several arrays and you want to smush them into one array then try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
use strict;
use Data::Dumper;
my @array1 = qw ( Socks Milly Poppy);
my @array2 = qw ( Tiddles Puss Ivan);
#Reference them in a new array
my @array3 = (\@array1, \@array2);
print @array3; #Prints the references
print $array3[0][2]; #Prints "Poppy"
#Adding elements is easy
push (@{$array3[0]}Â Â Â , "Thor");
print Dumper @array3;
#Returns
# $VAR1 = [
#Â Â Â Â Â Â Â Â Â 'Socks',
#Â Â Â Â Â Â Â Â Â 'Milly',
#Â Â Â Â Â Â Â Â Â 'Poppy',
#Â Â Â Â Â Â Â Â Â 'Thor'
#Â Â Â Â Â Â Â ];
# $VAR2 = [
#Â Â Â Â Â Â Â Â Â 'Tiddles',
#Â Â Â Â Â Â Â Â Â 'Puss',
#Â Â Â Â Â Â Â Â Â 'Ivan'
#Â Â Â Â Â Â Â Â ];
|
Array of Hashes
Want to store several hashes in an array? Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
use strict;
use Data::Dumper;
my %hash1 = ("Joe" => "Socks", "Peter" => "Milly", "Matt" => "Poppy");
my %hash2 = ("Mr X" => "Tiddles", "Mr Y" => "Thor", "Mrs C" => "Steve");
my @array = (\%hash1, \%hash2);
print @array; #Prints the references
print $array[1]{"Mr X"}; #Prints "Tiddles"
#Adding a new hash is easy
push (@array, {"Mrs A" => "Margret"});
print Dumper @array;
#Returns
#$VAR1 = {
# 'Joe' => 'Socks',
# 'Matt' => 'Poppy',
# 'Peter' => 'Milly'
# };
#$VAR2 = {
# 'Mr X' => 'Tiddles',
# 'Mrs C' => 'Steve',
# 'Mr Y' => 'Thor'
# };
#$VAR3 = {
# 'Mrs A' => 'Margret'
# };
|
Hash of Arrays
Storing arrays as a value with a key? Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
use strict;
use Data::Dumper;
my @array1 = qw ( Mats Hats Bats);
my @array2 = qw ( Milly Socks Poppy);
my %hash = (
"Rhymes" => \@array1,
"Cats" => \@array2
);
print $hash{"Cats"}[1]; #Prints Socks
print @{$hash{"Cats"}}; #Prints @array2
push (@{$hash{"Rhymes"}}, "Sacks");
print Dumper %hash;
#Returns
#$VAR1 = 'Rhymes';
#$VAR2 = [
# 'Mats',
# 'Hats',
# 'Bats',
# 'Sacks'
# ];
#$VAR3 = 'Cats';
#$VAR4 = [
# 'Milly',
# 'Socks',
# 'Poppy'
# ];
|
Hash of Hashes
Nesting hashes is a-OK.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
use strict;
use Data::Dumper;
my %hash1 = (1 => "Mats", 2 => "Hats", 3 => "Bats");
my %hash2 = ("Joe" => "Socks", "Pete" => "Milly", "Matt" => "Poppy");
my %hash = (
"Rhymes" => \%hash1,
"Cats" => \%hash2
);
print $hash{"Cats"}{"Joe"}; #Prints Socks
#Add a new key value to a hash
$hash{"Rhymes"}{4} = "Flats";
#Lets put in an array
my @array = qw (One Two Three);
$hash{"Numbers"} = \@array;
print Dumper %hash;
#Returns
#$VAR1 = 'Rhymes';
#$VAR2 = {
# '4' => 'Flats',
# '1' => 'Mats',
# '3' => 'Bats',
# '2' => 'Hats'
# };
#$VAR3 = 'Numbers';
#$VAR4 = [
# 'One',
# 'Two',
# 'Three'
# ];
#$#VAR5 = 'Cats';
#$VAR6 = {
# 'Joe' => 'Socks',
# 'Matt' => 'Poppy',
# 'Pete' => 'Milly'
# };
|
So as you can see, arrays and hashes are incredibly versatile.
See you later!