Perl | Hash Operations
Last Updated :
06 May, 2019
Improve
-
Prerequisite: Perl Hashes, Perl Hash
#!/usr/bin/perl
# Creating a simple hash containing
# different types of values
my %hash = ( # value type string
'MyVehicle' => 'Car',
# value type integer
'Model' => 1234,
# value type float
'Speed' => 60.7,
# value type hash
'Traffic' => {'Red' => 'Stop',
'Yellow' => 'Look and move',
'Green' => 'Go'},
# value type array
'AllVehicles' => ['Car', 'Cycle',
'Bus', 'Auto']);
# printing values stored
# at key 'Traffic' and 'AllVehicles'
print "Traffic : $hash{'Traffic'}\n";
print "AllVehicles : $hash{'AllVehicles'}\n";
Output:
Traffic : HASH(0x242af30) AllVehicles : ARRAY(0x24471f8)
-
Here, the data type at key Traffic is hash and at key AllVehicles is of type array. So, the output is the address of the first element of the hash and array respectively.
Further, many operations or manipulations can be performed on a hash which are explained below:
- Accessing the keys and values in a hash.
- Modifying the values of particular keys.
- Loop over to the hash.
- Perl foreach loop
- Perl while loop with each function
Operations on Hash
Perl hash operations include various operations which are acted upon hash to store and retrieve data more efficiently. The most commonly used operations in the hash are :Syntax : $hash_name{key_name};Example
# Perl program to demonstrate
# accessing of the hash values
my %hash = ('MyVehicle' => 'Car',
'Model' => 1234,
'Speed' => 60.7,
# value type hash
'Traffic' => {'Red' => 'Stop',
'Yellow' => 'Look and move',
'Green' => 'Go'},
# value type array
'AllVehicles' => ['Car', 'Cycle',
'Bus', 'Auto']);
# Creating array containing
# keys of the hash
@k = keys %hash;
# print all keys of the hash
print "Keys are : ";
print join(", ", @k), "\n";
# Creating array containing
# values of the hash
@v = values %hash;
# print value of all values
print "Values are : ";
print join(", ", @v), "\n";
# accessing individual values of the keys
print "Speed is : $hash{'Speed'}\n";
print "Yellow light indicates : $hash{'Traffic'}{'Yellow'}\n";
print "$hash{'AllVehicles'}[3] is a type of vehicle \n";
Output:
Keys are : AllVehicles, MyVehicle, Speed, Traffic, Model Values are : ARRAY(0x9361f8), Car, 60.7, HASH(0x919f30), 1234 Speed is : 60.7 Yellow light indicates : Look and move Auto is a type of vehicle
-
Modify hash elements
Values in a hash are not fixed, that is they are prone to change and Perl provide this ability to modify and update the values in a hash. For a given key, to modify or update its corresponding value use following syntax :
Syntax : $hash{'Key'} = modified_value;To Modify a given key without changing its corresponding value, simply create an additional key which is the modified key and assign value (for which you want this new key) to this key and then delete the previous key/value pair using delete keyword. Example :
# Perl program to demonstrate the
# Modification of an element of a Hash
# creating a hash
my %hash = ('MyVehicle' => 'Car',
'Model' => 1234,
'Speed' => 60.7,
# value type hash
'Traffic' => {'Red' => 'Stop',
'Yellow' => 'Look and move',
'Green' => 'Go'},
# value type array
'AllVehicles' => ['Car', 'Cycle',
'Bus', 'Auto']);
# previous value of key 'Model'
print ("Previous Model number is ",
$hash{'Model'}, "\n");
# modifying value of key 'Model'
$hash{'Model'} = 7717;
# new value of key 'Model'
print ("New Model number is ",
$hash{'Model'}, "\n");
# Changing key from 'MyVehicle' to 'Mine'
# without changing its corresponding value
@k = keys %hash;
# printing previous keys
print "Previous Keys are : \n";
print join(", ", @k), "\n";
$hash{'Mine'} = 'Car';
# deleting 'MyVehicle' key/value pair
delete $hash{'MyVehicle'};
@k_n = keys %hash;
print "New Keys are : \n";
# printing new keys
print join(", ", @k_n), "\n";
Output:
Previous Model number is 1234 New Model number is 7717 Previous Keys are : MyVehicle, AllVehicles, Model, Traffic, Speed New Keys are : Mine, Speed, Traffic, Model, AllVehicles
Syntax keys %HashBesides, Perl provides two ways to loop over all the elements in a hash.
# Perl program to demonstrate the
# looping over a hash using its keys
# creating a hash
my %hash = ('MyVehicle' => 'Car',
'Model' => 1234,
'Speed' => 60.7,
# value type hash
'Traffic' => {'Red' => 'Stop',
'Yellow' => 'Look and move',
'Green' => 'Go'},
# value type array
'AllVehicles' => ['Car', 'Cycle',
'Bus', 'Auto']);
# Case 1: When hash is not large
# for loop to loop over the hash
foreach my $key (keys %hash)
{
# do stuff
$value = $hash{$key};
print "Value of $key is $value\n";
}
# Case 2: When hash is very large
# traversing the hash using "each" function
while(($key, $value) = each (%hash))
{
# do stuff
$value = $hash{$key};
print "Value of $key is $value\n";
}
Output:
In the above example, the code gives address of the first element of the array and the hash stored at key 'AllVehicles' and 'Traffic' respectively. To overcome this, we have to loop over inside the array and hash also.
Example
Value of Model is 1234 Value of MyVehicle is Car Value of Traffic is HASH(0x1049f30) Value of AllVehicles is ARRAY(0x10661f8) Value of Speed is 60.7 Value of Model is 1234 Value of MyVehicle is Car Value of Traffic is HASH(0x1049f30) Value of AllVehicles is ARRAY(0x10661f8) Value of Speed is 60.7
# Perl program to demonstrate the
# looping over a multidimensional hash
# creating a hash
my %hash = ('MyVehicle' => 'Car',
'Model' => 1234,
'Speed' => 60.7,
# value type hash
'Traffic' => {'Red' => 'Stop',
'Yellow' => 'Look and move',
'Green' => 'Go'},
# value type array
'AllVehicles' => ['Car', 'Cycle',
'Bus', 'Auto']);
# Loop over the key storing array
my @array = @{$hash{'AllVehicles'}};
print "AllVehicles include \n";
# for loop to loop over the array
foreach my $ele (@array)
{
print "$ele\n";
}
# Loop over the key storing hash
print "\nTraffic includes\n";
# for loop to loop over the hash
foreach my $val(keys %{$hash{'Traffic'}})
{
print "Key : $val, value : $hash{'Traffic'}{$val}\n";
}
Output:
AllVehicles include Car Cycle Bus Auto Traffic includes Key : Yellow, value : Look and move Key : Green, value : Go Key : Red, value : Stop