Updated April 5, 2023
Introduction to Perl array of hashes
In Perl array of hashes is one of the feature and concept related to the hash-map technique, and it has a set of bunched datas that will be accessed by sequential order; also, each set of bundled datas will contain key-value pairs that are related to the hash-map technique mainly it will be accessed and used in the less frequent nature in an array of hashes the key will be uniqueness so it cannot accept the duplicate keys for each set of keys have the separate values so that it will point out the single key with values.
Syntax:
The Perl script hashing is one of the processes and techniques to convert the user keys into another value; with the help of the hash function, the hash value is generated. The array is a sequential data storage in the memory; the hash type of variables is mentioned using the percent sign(%) so that it will be identified easily; also, the array variables have their own prefix with the unique set of keys. If the key exists, it will be automatically overwritten in the memory.
#! /usr/bin/perl
my %vars = ( key1 => value1, key2 => value2 ….);
my @vars1 = ({ key1 => value1, key2 => value2 ….});
---some perl script logics depends upon the requirement------
The above code is the basic syntax for creating the hash from the array. We used Perl variables with the array set of methodology and hashing technique to allocate the datas to the memory location.
How the array of hashes work in Perl?
Whenever we want to push the hashes onto the arrays, they will be stored as the reference of the array variables. So that each array set of elements will have their own references will the same as the original hash variables. I suppose we want to change the value of the hash storage; it will be automatically changed for all array elements, so whenever we want to change the values in hash storage, the changes are reflected in the arrays. So the reference of all the variables will be the same hash values. Suppose we want to create a new hash that time the values are pushed to it, it will be trying for the same in anonymous hash storage.
In this way, the reference of the variables will be pushed at each time the hash variable’s values initialize. Sometimes we want to push the hash technique to the arrays more than once; when we change the hash, it will change all the references that are inside of the array elements. Like that array of hashes will be less frequently used in the Perl script applications, the process will take more time, and it will handle the big amount of datas in the single variables by using the array of hashes techniques.
Examples of Perl array of hashes
Here are the following example mention below
Example #1
Code:
#!/usr/bin/perl
my %vars = ( k1 => 'jhd ajds jdjg wkj kwe kwej kweh wkeb kweb wkqeb kwqjeh wkejb ',
k2 => 'Welcome',
k3 => 'To',
k4 => 'My Domain',
k5 => 'Have a Nice Day' );
@vars1 = keys %vars;
$varsizes = @vars1;
print "Welcome To My Domain $varsizes\n";
$vars2{'k3'} = 'To';
@vars1 = keys %vars;
$varsizes = @vars1;
print "Have a Nice Day $varsizes\n";
delete $vars2{'k3'};
@vars1 = keys %vars;
$varsizes = @vars1;
print "After Deleting the key your array size is: $varsizes\n";
$vars2{'k1'} = 'jhd ajds jdjg wkj kwe kwej kweh wkeb kweb wkqeb kwqjeh wkejb';
for(keys %vars){
print("Your Total key size for $_ is $vars2{$_}\n");
}
Output:
In the above example, we have declared the variable values as array format. It will declare and to store it in the hash map technique called key-value pairs. In that, we want to add or remove the keys by using the default methods. Here we want to delete the key by using the delete keyword with the help of for loop; the keys are iterated and displayed after the removal operations.
Example #2
Code:
my %vars = (
k1 => 'Welcome',
k2 => 345,
k3 => 'To My Domain'
);
my %vars1= (
k1 => 'jasgvyt6347 hdg wjhg kjweg kwjeh kqwegw kqwhew kqwg kqwehg486 jweg9848767 kjweg984386 kewg4838 hewjreg48 hewr',
k2 => 4865,
k3 => 'To My Domain'
);
my %vars2 = (
k1 => 'jasg34 r 34 4',
k2 => 49483,
k3 => 8743654934
);
my %vars3 = (
k1 => '465 cjhf9345 hsd jkeh',
k2 => 4948,
k3 => 87423345
);
my %vars4 = (
k1 => 'Hi',
k2 => 2536,
k3 => 'have a Nice day'
);
my %vars5 = (
k1 => 'jkgdf',
k2 => 49784,
k3 => 'Once Again Thank you'
);
my @vars7 = (\%vars,\%vars1,\%vars2,\%vars3,\%vars4,\%vars5);
my $vars8 = "%-13s %-17s %-237s %-16s %-13s %-23s\n";
foreach my $vars6 (@vars7) {
printf $vars8, @$vars6{qw(k1 k2 k3)};
}
Output:
In the second example, we used the basic key-value pairs technique for the variables $vars1 to $vars5. Using the foreach loop, the values are iterated and stored in the new variable using the qw default keyword and print the values on the output console.
Example 3
Code:
# !/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %vars = ('example1' => {
'k1' => 'kjeh',
'k2' => 'hdj',
'k3' => 'dj',
},
'example' => {
'k1' => 'iwqyeg',
'k2' => 'jhdgs',
'k3' => 'jsd',
},
'example3' => {
'k1' => 'wkg',
'k2' => 'ksjd',
'k3' => 'skdjg',
},
'example4' => {
'k1' => '8347',
'k2' => 'sdj',
'k3' => 'kjsd',
},
'example5' => {
'k1' => 'kjdsg',
'k2' => 'sjdh',
'k3' => 'shdkj',
},
);
print Dumper(\%vars);
Output:
In the final example, we used the same hashing technique with array elements. But we used an additional Dumper function for referencing the data structure elements so that the elements are arranged with some default orders with a pre-defined variable called “$VAR1” and also “\” by using back-slash operator, it calculates the space for each key and value pair elements.
Conclusion
We used a lot of functionalities in Perl script among that array of hashes is one of the concepts and used less frequently in the application. It is mostly used in big-enterprise data applications to handle a huge amount of datas to store and retrieve the datas in the heap memory.
Recommended Articles
This is a guide to the Perl array of hashes. Here we discuss How the array of hashes work in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –