In previous article we have discussed about the Array . Now , in this article we will cover the Linked List or the HASH-Table i.e Associative Array in PERL.
Associative Array / Hash in PERL :
- An associative array(hash) is an array of scalar values associated with the subscripts/index that are not 0, 1, 2, 3 ...
- Rather subscripts/index are any scalar values with no particular order.
- These subscripts are called "keys". Corresponding array elements are called "values".
- A "value" is mapped to its corresponding "key".
- Thus a hash is a table of uniquely mapped key-value pairs. Generically it is called as hash table or hash.
- Hash variables always start with % (percent) symbol.
Creation of an Associative Array :
The HASH can be created in below ways :-
1. Entire hash variables(key&value) are referenced using the “%” ;
my %hash = ("name", "Billy",
"age", 21 ,
"occupation", "student"
);
my %hash = ("name" => "Billy",
"age" => 21,
"occupation" => "student"
);
2. Individual hash variables (scalars) are mapped using $name{key} = value ;
$hash{"name"}= "Billy" ; # note we are using { } and not using [ ]
$hash{"age"} = 21;
$hash{"occupation"} = "student";
Accessing Hash Key/Value after Hash Creation :
The key-value pair can be access in below ways :-
1. When you know the key ;
print "$hash{"name"}"; #prints Billy
print "$hash{"age"}"; #prints 21;
print "$hash{"occupation"}"; # Prints student
2. When you do not know the keys;
my @arr = keys(%hash); #returns list of keys
foreach my $key (@arr) {
print “Key = $key has Value = $hash{$key}\n”;
}
3. Get the Key Value pair at once using each function ;
while (($key,$value) = each(%hash)) {
print “Key = $key has Value = $value \n”;
}
Associative Array / Hash Functions :
There are few functions are there to operate exclusively on HASH :
- keys - returns a list of the keys
- my @keyArr = keys(%hash);
- values - returns a list of the values
- my @valArr = values(%hash);
- each - returns a (key, value) pair in sequence
- delete - removes a key-value pair from a hash
- delete $hash{"key1"} ;
- %b = %a; # copies hash into other
- %a = reverse %b; # swap keys and values, values should be unique
Array and Hash : Mutual Conversions :
We can mutually convert the array and hash in to each other as :-
- my @arr = %hash; # converts a hash into a array
- # Not necessarily in that order, but always with keys
- # and values paired adjacently
- my %newHash = @arr; # converts array back to a hash
- my %arr = ("key1", val1 ,"key2", val1, "key3", val3); # creates a hash from list
- my @arr{"key1","key2","key3"} = (val1,val2,val3); # same thing
References in PERL :
- PERL allows you have "hard" references to any piece of data or code.
- Any scalar may hold a hard reference.
- Arrays and hashes contain scalars.
- Hence one can now easily build nested data structures as :
- arrays of arrays, arrays of hashes , hashes of arrays, arrays of hashes of functions, and many other combinations..…
- [ ITEMS ] makes a new, anonymous array, and returns a reference to that array.
- { ITEMS } makes a new, anonymous hash, and returns a reference to that hash.
Backslash Operator & ref() function :
By using the backslash operator on a variable, subroutine, or value typically creates another reference to a variable.
Backslash Operator (\) is similar to the address-of-operator(&) in C.
$scalarref = \$foo; #reference to a scalar
$arrayref = \@ARGV; #reference to an Array
$hashref = \%ENV; #reference to a Hash
$constref = \186_282.42; #reference to a constant
$coderef = \&handler; #reference to a piece of code
$objRef = new Student("Name", "Surname" , "Roll"); #reference to a object
There is a inbuilt function ref() which will check & return the reference type
Usage : ref($refVar) #$refVar is the reference variable in concern
Reference Example
Through this example , we will demonstrate the use of ref() function and see its output in various cases :
Upon execution this code gives the below output which is self explanatory :
With the below code-example we do the Anonymous Referencing & De-Referencing :
Request you to run this code at your end .
De-Referencing : Arrow Notation :
# Accessing A Value
print "$ArrayRef->[0]";
print "$HashRef->{"KEY"}";
# Assigning A Value
$ArrayRef->[0] = "January"; # Array element
$HashRef->{"KEY"} = "VALUE"; # Hash element
Unnamed Associative Array/Hash :
With the below code example we will create and access unnamed hash :
Upon execution the code gives the below output :
The entire article is well narrated in the below video :