An array of hashes in the Perl language stores data that you would like to access in a sequential manner. Each of the array indices has key/value pairs for hashes.
The general syntax for arrays that consist of hashes is as shown below:
@Arrays = (
{ key1 => value1,
key2 => value2,
},
{
key1 => value3,
key2 => value4,
},
);
The following examples will help you understand the arrays of hashes in greater detail.
To add another hash to an array, we first initialize the array with our data. Then, we use push
to push the new hash to the array. The new hash should have all of its data. As shown below, you can see the difference between the two arrays before and after pushing a new hash.
@Arrays = ({ name => "Harry Potter",author => "J K Rowling",},{name => "Gone Girl",author => "Gillian Flynn",},);print "Before pushing to the array \n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val} "; }print "\n";}push @Arrays, {name => "Gone with the wind",author => "Margaret Mitchell"};print "\nAfter pushing to the array\n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val} "; }print "\n";}
To populate from a file, we must first open
the file and read each line. In the code below, each line of data is called dataline
.
According to data.txt
, each line is first split by a comma and then a =
. The comma separates different keys, and the =
separates a key from its value in data.txt
.
Once split, we can add this data to a hash and then push the respective hash to an array. As shown below, once all hashes have been pushed, we can print our array to reflect the data in the file.
@Arrays = ();my %data;open(my $books, "<data.txt" ) or die "cannot open data.txt\n";while( my $dataline = <$books> ) {$rec = {};my @data = split(',', $dataline);my @data1 = split('=', $data[0]);my @data2 = split('=', $data[1]);$rec->{$data1[0]} = $data1[1];$rec->{$data2[0]} = $data2[1];push @Arrays, $rec;}close($books);print "Array:\n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val}\n"; }}
To add a new member to the hash, we can access a hash by its index in the array and define the new member as shown below. The print before and after displays how the array has changed after adding new members to the array.
@Arrays = ({ name => "Harry Potter",author => "J K Rowling",},{name => "Gone Girl",author => "Gillian Flynn",},);print "Before adding to the hash \n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val} "; }print "\n";}$Arrays[0]{year} = "1997";$Arrays[1]{year} = "1997";print "\nAfter adding to the hash \n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val} "; }print "\n";}
Printing a hash is simple, as one just has to run a for
loop through the keys of hashes of an array and print both the keys and their values, as shown below.
@Arrays = ({ name => "Harry Potter",author => "J K Rowling",},{name => "Gone Girl",author => "Gillian Flynn",},);print "Printing\n";for $key ( @Arrays ){ for $val ( keys %$key ){ print "$val=$key->{$val} "; }print "\n";}
Free Resources