A hash is a data structure that associates keys with values. The primary operation it supports efficiently is a lookup: given a key (e.g. an employee's id), find the corresponding value (e.g. that employee's name). It works by transforming the key using a hash function into a hash, a number that is used as an index in an array to locate the desired location ("bucket") where the values should be.
To define a hash in Perl, we use the percent (%) symbol before the name.
There are numerous methods to iterate a hash, here are some of the following.
Method 1
Using each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.
Method 2
Using keys with a for / foreach loop.
Method 3
Using map.
Method 1
Method 2
To define a hash in Perl, we use the percent (%) symbol before the name.
Declaring/Initializing/Clearing/Empty A Hash
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
my %hash = () # initializing an empty hash / Clear,Empty an existing hash
## HASH REFERENCE
my $hash_ref = \{}; # reference to an empty hash
Adding A Key/Value Pair To A Hash
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
my $hash_ref = \%hash;
$hash{'myKey'} = 'myValue'; # quotes around the keys can be omitted when the keys are identifiers.
$hash{$myKey} = $myValue; # hash, using variables
## ADDING TO A HASH REFERENCE
$hash_ref->{'myKey'} = 'myValue';
$hash_ref->{$myKey} = $myValue;
Deleting A Single Key/Value Pair
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
my $hash_ref = \%hash;
delete $hash{$key};
## DELETE HASH REFERENCE
delete $hash_ref->{$key};
Iterating Over The Hash Key/Value Pair
There are numerous methods to iterate a hash, here are some of the following.
Method 1
Using each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
## Add some dummy data to the hash here
while(my ($key, $value) = each(%hash))
{
print "$key => $value\n";
}
my $hash_ref = \%hash;
## HASH REFERENCE
while(my ($key, $value) = each(%$hash_ref))
{
print "$key => $value\n";
}
Using keys with a for / foreach loop.
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
## Add some dummy data to the hash here
for my $key ( keys %hash )
{
my $value = $hash{$key};
print "$key => $value\n";
}
my $hash_ref = \%hash;
## HASH REFERENCE
for my $key ( keys %$hash_ref )
{
my $value = $hash_ref->{$key};
print "$key => $value\n";
}
Using map.
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
## Add some dummy data to the hash here
map {print "$_ => $hash{$_}\n";} keys %hash;
my $hash_ref = \%hash;
## HASH REFERENCE
map {print "$_ => $hash->{$_}\n";} keys %$hash;
Size Of A Hash
Method 1
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
## Add some dummy data to the hash here
print "size of the hash: " . keys( %hash ) . ".\n";
Code: Perl
#!/usr/bin/perl
use strict;
my %hash; # declaring a hash
## Add some dummy data to the hash here
my $hash_ref = \%hash;
my $i = 0;
$i += scalar keys %$hash_ref; # method 1: explicit scalar context
$i += keys %$hash_ref; # method 2: implicit scalar context
Sorting Hashes by Key
Code: Perl
#!/usr/bin/perl
use strict;
my %hash = (23,'pradeep',26,'shabbir',34,'asha',15,'tanaz'); # declaring a hash
# sort using foreach
foreach my $key (sort keys %hash)
{
print "$key => $value \n";
}
Sorting Hashes by Values
Code: Perl
#!/usr/bin/perl
use strict;
my %hash = (23,'pradeep',26,'shabbir',34,'asha',15,'tanaz'); # declaring a hash
# sort using foreach, ascending sort
foreach my $key (sort {$hash{$a} cmp $hash{$b}} keys %hash)
{
print "$key => $value \n";
}
# sort using foreach, descending sort
foreach my $key (sort {$hash{$b} cmp $hash{$a}} keys %hash)
{
print "$key => $value \n";
}


