Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

What are arrays of hashes in Perl?

Samia Ishaque

Overview

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,
},
);

Examples

The following examples will help you understand the arrays of hashes in greater detail.

How to add another hash to the array

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"; 
} 

How to populate an array of hashes from a file

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.

main.perl
data.txt
@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"; }  
} 

How to add new members to an existing hash

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"; 
}

How to print an array of hashes

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"; 
} 

RELATED TAGS

perl

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring