Perl

Perl Array of Hashes

The Perl hash is used to store the data based on key-value pairs. Perl hash works like the associative array of the other programming languages. When each element of the Perl array is declared with the hash data, that type of array is called an array of hashes. If it is required to store a large amount of data in sequential order and each index contains the hash data of multiple key-value pairs, the Perl array of hashes is used. Different uses of the Perl array of hashes are shown in this tutorial.

Syntax:

The syntax of an array of hashes is given in the following. Here, each element of the array is a hash data:

array = ({hash1}, {hash2},, {hash3})

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.

#!/usr/bin/perl

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:

p1

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.

#!/usr/bin/perl

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:

p2

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=Programming_Perl, author_name=Larry_Wall

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.

#!/usr/bin/perl

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:

p3

Conclusion

The concept of using an array of hashes in Perl is explained in this tutorial using multiple Perl examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.