Wednesday, December 5, 2012

10 examples of initializing a Hash variable in Perl

 Hash'es  is one of the most important concepts in Perl. Hashes are associative arrays where the data is stored in the form of key-value pairs. The big advantage of a hash is faster look-up compared to arrays. In this article, we will see the how we can populate / initialize a hash variable in different scenarios:

1. Regular way of initializing a hash :
my %h1=("Jan" => 31, "Feb" => 28, "Mar" => 31);
  This is the normal way of populating hash where-in every set is a key-value pair. The operator separating the key-value pair '=>' is called Fat-comma operator.
  After the initialization, the data in the hash will be stored as shown below where 'Jan','Feb' and 'Mar' are the keys of the hash and values against it are their respective values associated.
'Jan' => 31
'Feb' => 28
'Mar' => 31
2. Without quotes for keys:
my %h1=(Jan => 31, Feb => 28, Mar => 31);
  The double quotes are optional when the fat comma is used. It is because the fat comma operator automatically considers the key as a string.

3.  Without Fat comma:
my %h1=("Jan",31,"Feb",28,"Mar",31);
   Hash can also be initialized like an array. Every 2 values are combined as a key-value pair.

4. Using hashes in array context:
my @key = qw(Jan Feb Mar);
my @val = (31,28,31);
my %h1;
  The keys are stored in a an array @key, and the values are stored in an array @val. The hash is populated using the hash in array context. The respective elements in the key array are mapped to elements in val array and the hash is formed.

5. Directly using an array:
my @arr = ("Jan",31,"Feb",28,"Mar",31);
my %h1=@arr;
  This is actually a way of converting an array to a hash. Just by assigning an array to a hash gets an array converted to a hash. From the array, every 2 values are taken and formed a key-value pair for the hash.

6. Using the map function to construct hash:
my @arr = ("Unix","Linux","AIX");
my %h1=map {$_ => 1}@arr;
    This is one of the ways used to check for existence of an element in an array. Instead of scanning for every element of an array, an hash is created with every array element as the key and value being some constant, say 1. With hash, an element can be checked for its existence directly using the exists function.
The hash %h1 will contain:
'Unix' => 1
'Linux' => 1
'AIX' => 1
7.  Using the map with key values serialized:
my $i=1;
my @arr = ("Unix","Linux","AIX");
my %h1=map {$_ => $i++}@arr;
  This is almost same as the last example, except that the value is a sequence provided using a variable by incrementing it.
The hash %h1 will contain:
'Unix' => 1
'Linux' => 2
'AIX' => 3
8. Hash with values being length of the keys:
my @arr=("Unix","Linux","AIX");
my %h1 = map {$_ => length($_)}@arr;
  In this, instead of a serial number as the key, length of the key is maintained as the value for the hash using the length function.
The hash %h1 will contain:
'Unix' => 4
'Linux' => 5
'AIX' => 4
9. Hash from array where keys are the 1st half, and values in the 2nd half:
my @arr=("Unix","Linux","AIX",2,3,5);
my %h1;
@h1{@arr[0 .. $#arr/2]}=@arr[@arr/2 .. $#arr];
   This is used in cases where we have arrays in which the first half contains the keys and the 2nd half contains the values. The hash is created by retrieving the first half which ends at $#arr/2 and the values starts from @arr/2 till the last index($#arr) of the array.
The hash %h1 will contain:
'Unix' => 2
'Linux' => 3
'AIX' => 5
10. Populating hash with values from a file:
my %h1;
open my $fh , '<' , 'file.txt' or die $!;
        my ($k,$v)=split;
  This is a commonly used way when the data for the hash is coming from a file(assuming the file is space separated and the 1st column contains the key, the 2nd containing the value). The columns are separated using the split function and the hash is formed using the separated values.

No comments:

Post a Comment