As we have earlier discuss bit about Hash, In this post we will discuss in detail about HASH Data Type in Perl. Hash variables are preceded by a percent (%) sign. These are key/value pairs, if you like to access element from hash you have to refer its Hash variable name precedence with $ sign and need to followed with key to which value is bind. Hash value are not saved with their index value, they are bind to keys, So Hash key-value pair index or position get changed all time.

Let’s see some of example for Hash and further will discuss about in detail.

#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@key = keys %mark;

@value = values %mark;

print "Hash \%mark has key:  @key\n";
print "Hash \%mark has values: @value\n";

print "Marks of Math: ", $mark{Math},"\n";

This will produce below output.

Hash %mark has key:  Social study English Science Hindi Math
Hash %mark has values: 69 76 90 83 85
Marks of Math: 85

With above example, it is quit clear that Hash are key value pairs where keys has some values placed which could access through keys. In above code we have already create one Hash variable, but what if we need to insert another pair inside the hash variables. Let’s see how we could append Hash key/value pairs in variable.

Extracting keys and values

Although we have already used functions @keys and @values, but it was easy enough to understand as it was quit like array declaration which we already discuss in previous post. Actually with these functions we could identify Keys and values of Hash variable and save them in a array variable which we could easily use like an array element.

As used keys function, we can use values function to extract Hash variable values.

# cat 1_hash_example
#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@key = keys %mark;

print @key[0],"\n" ,@key[1],"\n", @key[2],"\n", @key[3],"\n", @key[4],"\n";

So Here we used key function and extract its values in a array variable and named it @key. Now we can call every Subject (which is key in %mark) from Hash variable.So let’s see its output.

# perl 1_hash_example 
Hindi
Social study
Math
English
Science

One thing to note here, every time we run this command, we get different order because as already said Hash key-value will never saved as per index base.

Insert a key-Value pair in Hash variable

As said above Creation of Hash variable is already mentioned in above code, but what if we need to add some key-value pair in Hash value. We can insert it one by one as mentioned in below code.

#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@key = keys %mark;
@value = values %mark;

print "Before Addition\n";

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n\n";

$mark{'Physics'}='96';

print "After Addition\n";
@key = keys %mark;
@value = values %mark;

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n";

In above code we just add another key-value pair in between code which increase key-value pair count and also mentioned count before and after addition. Here we one another subject “Physics” and marks obtained in same. Code output will show it.

# perl 1_hash_example 
Before Addition
Total number of Hash %mark's  key :5
Total number of Hash %mark 's Value :5

After Addition
Total number of Hash %mark's  key :6
Total number of Hash %mark 's Value :6

Second way to create Hash creation

There is another way to create hash variable which we felt more clear for Hash type variables, In this declaration we can mention one pair per line which is quit more presentable as compared to previous one which we already saw in above code. So let’s see below code.

#!/usr/bin/perl

%mark=(
"Math" => "85",
"Science" => "90",
"English" => "76",
"Social Study" => "69",
"Hindi" => "83"
);

@key = keys %mark;
@value = values %mark;

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n";

Above code would have below output

Total number of Hash %mark's  key :5
Total number of Hash %mark 's Value :5

Access Hash elements

In this post, we earlier saw how to declare Hash variables and add some pair-value in same. Now in this section we will see how to access particular Hash elements from Hash variables.

#!/usr/bin/perl

#%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

%mark=(
"Math" => "85",
"Science" => "90",
"English" => "76",
"Social Study" => "69",
"Hindi" => "83"
);

print "Mark of Math :", $mark{'Math'},"\n";

This code will show below output

Mark of Math :85

So with this code we now know how to access single or multiple key-value pair from Hash variable. Here we just access one value (Math’s marks) from Hash variable.

Extract only specific values

We can extract specific key values from Hash variable like an defined array, we have example of code for same mentioned below.

#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@mark_only=@mark{Math,Science,English,"Social Study",Hindi};

print "Mark only: @mark_only\n";

Output would be as mentioned below.

Mark only: 85 90 76  83

Hash Size

we already see how to identify Hash size, although there is no straight method to find out it from Hash variable, what we can do we can take out keys or value in array variable and now array has method to find out array size. So we will calculate array size that came out of Hash variable. Let’s see below code for example.

#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@key = keys %mark;
@value = values %mark;

print "Before Addition\n";

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n\n";

$mark{'Physics'}='96';

print "After Addition\n";
@key = keys %mark;
@value = values %mark;

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n";

In above code we just add another key-value pair in between code which increase key-value pair count and also mentioned count before and after addition. Code output will show it. This is same as mentioned as earlier as well.

Output of Above code

# perl 1_hash_example
Before Addition
Total number of Hash %mark's  key :5
Total number of Hash %mark 's Value :5

After Addition
Total number of Hash %mark's  key :6
Total number of Hash %mark 's Value :6

So we also now know how to add one key-value pair in Hash variable, let’s see how to remove one key-value pair from Hash variable.

Removal of Hash variable

In Perl, we have delete function which used to remove key-value pair from Hash variable, for better clarity refer below code example.

#!/usr/bin/perl

%mark=('Math','85','Science','90','English','76','Social study','69','Hindi','83');

@key = keys %mark;
@value = values %mark;

print "Before Addition\n";

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n\n";

$mark{'Physics'}='96';

print "After Addition\n";
@key = keys %mark;
@value = values %mark;

print "Total number of Hash \%mark's  key :", scalar @key,"\n";
print "Total number of Hash \%mark 's Value :", scalar @value,"\n\n";

delete $mark{'Physics'};
print "After Deletion\n";
@key = keys %mark;

@value = values %mark;
#print $mark{'Physics'},"\n";

print "Total number of Hash \%mark's  key :", scalar @key,"\n";

print "Total number of Hash \%mark 's Value :", scalar @value,"\n";

Output comes out from above code.

Before Addition
Total number of Hash %mark's  key :5
Total number of Hash %mark 's Value :5

After Addition
Total number of Hash %mark's  key :6
Total number of Hash %mark 's Value :6

After Deletion
Total number of Hash %mark's  key :5
Total number of Hash %mark 's Value :5

Next