Syntax:
The syntax of an array of hashes is given in the following. Here, each element of the array is a hash data:
Different Uses of the Array of Hashes
The ways of declaring, accessing, and inserting the data in an array of hashes using Perl script are shown in this part of the tutorial.
Example 1: Declaring and Accessing an Array of Hashes
Create a Perl file with the following script that declares an array of hashes with two hash elements and print some values of the array by defining the index values. Here, two hash values of the first index and three hash values of the second index of the array are printed.
use warnings;
use strict;
#Declare an array of hash
my @products = (
{
id => "p-1837",
name => "Android TV",
brand => "Samsung",
size => "40 inches",
price => 40000
},
{
id => "p-7856",
name => "AC",
brand => "General",
size => "2 Ton",
price => 90000
}
);
#Print two values of the first index of the array
print "Product Name: ".$products[0]{'name'}."\n";
print "Brand Name: ".$products[0]{'brand'}."\n";
#Print three values of the second index of the array
print "\nProduct Name: ".$products[1]{'name'}."\n";
print "Size: ".$products[1]{'size'}."\n";
print "Price: ".$products[1]{'price'}."\n";
Output:
The following output appears after executing the script:
Example 2: Print All Elements of an Array of Hashes Using a Loop
Create a Perl file with the following script that accesses all elements of the array of hashes using a “for” loop. An array of two hash elements is declared in the script. A “for” loop is used to access each key of each hash element by sorting the hash keys and printing all keys with the values.
use warnings;
use strict;
#Declare an array of hash
my @products = (
{
id => "p-1837",
name => "Android TV",
brand => "Samsung",
size => "40 inches",
price => 40000
},
{
id => "p-7856",
name => "AC",
brand => "General",
size => "2 Ton",
price => 90000
}
);
#Print all elements of the array of hashes by using the loop
for (my $i = 0; $i <= $#products; $i++) {
for my $value ( sort keys %{ $products[$i] } ) {
print "$value = $products[$i]{$value}\n";
}
print "\n";
}
Output:
The following output appears after executing the script:
Example 3: Insert the Data into an Array of Hashes from a File
Create a text file with the following content to insert the values as the hash values in an array. According to the content, two hash elements are inserted into the array.
books.txt
book_name=Modern_Perl, author_name=Chromatic
Create a Perl file with the following script that reads the content of the “books.txt” file and insert the two hash values into an array of hashes. An array of one hash value is defined at the beginning of the script. Then, the “books.txt” file is opened for reading and the content of each line is splitted and formatted in the hash format to insert into the previously defined array. The content of the array is printed later.
use warnings;
use strict;
use Data::Dumper;
#Declare an array of hash
my @books = (
{
book_name => "Perl Best Practices",
author_name => "Damian Conway"
},
);
print "Array of hash before inserting data: \n".Dumper(\@books);
#Open the file for reading
open(IN, "<", 'books.txt') or die "File does not exits.\n";
#Read the file using the loop
while(<IN>)
{
#read each line of the file
my $line = $_;
#Define variable store the hash value
my $data = {};
#Iterate each line and split the value
for $line ( split ) {
(my $key, my $value) = split /=/, $line;
#Replace the '_' by a space in the content
$value =~ s/_/ /;
#Store the value in the variable
$data->{$key} = $value;
}
#Insert the hash data in the array
push @books, $data;
}
#Close the file handler
close(IN);
#Print the dumped variable to check whether the hash values are inserted or not
print "\nArray of hash after inserting data: \n".Dumper(\@books);
Output:
The following output appears after executing the script:
Conclusion
The concept of using an array of hashes in Perl is explained in this tutorial using multiple Perl examples.