PERL/C2/Hash-in-Perl/English
Title Of Script: Hash in Perl
Author: Amol Brahmankar
Keywords: Hash in perl, Associative Array in Perl video tutorial.
|
|
Slide | Welcome to the spoken tutorial on Hash in Perl. |
Slide: Learning Objectives | In this tutorial, we will learn about;
|
Slide: System Requirements | For this tutorial, I am using
You can use any text editor of your choice. |
Slide: Prerequisites | To practise this tutorial, you should have knowledge of Variables & Data Structures in Perl.
|
Slide | * Hash is an unordered collection of data
|
Slide | The syntax of hash is as shown. |
Let us see how to get the value of a key from hash. | |
Slide
|
The syntax for accessing the value of a key is
|
Let us understand hash using a sample program. | |
Switch to gedit with perlHash.pl opened | I have already typed the code in perlHash dot pl file in gedit. |
Gedit
'Name' => 'John', 'Department' => 'Finance' );
|
Type the code as shown in your perlHash dot pl file.
<<pause>>
|
%hash
|
Hash in Perl is declared with percentage sign.
|
Slide | Press Ctrl + S to save the file. |
Switch to terminal
|
Then switch to the terminal and execute the Perl script as
|
Highlight the output on the terminal
|
The output is as shown on terminal. <pause>
|
Now let us see add and delete of keys from a hash. | |
Slide | The syntax for
1. adding key is dollar hashName open curly bracket single quote KeyName single quote close curly bracket equal to value semicolon 2. deleting key is delete dollar hashName open curly bracket single quote KeyName single quote close curly bracket semicolon |
Now, let us understand this using a sample program. | |
Terminal hashKeyOperations dot pl | I have already typed the code in hashKeyOperations dot pl file in gedit. |
Type the code as shown in your hashKeyOperations dot pl file.
<<pause>> | |
#!/usr/bin/perl
use Data::Dumper;
%hash = ( 'Subject' => 'Math', 'Marks' => 98 );
print Dumper \%hash;
$hash{'Name'} = 'Amol'; $hash{'RollNumber'} = 12;
print Dumper \%hash;
print Dumper \%hash; |
This is the declaration of hash.
It is like assigning a value to a variable.
We need to pass the key to delete it.
|
Press Ctrl+S to save the file. | |
Terminal
perl hashKeyOperations.pl |
Switch to the terminal and execute the Perl script as
perl hashKeyOperations dot pl and press Enter. |
Hash before adding keys
$VAR1 = { 'Subject' => 'Math', 'Marks' => 98 }; Hash after addition of Name & RollNumber keys $VAR1 = { 'Subject' => 'Math', 'RollNumber' => 12, 'Marks' => 98, 'Name' => 'Amol' }; Hash after deletion of RollNumber key $VAR1 = { 'Subject' => 'Math', 'Marks' => 98, 'Name' => 'Amol' }; |
Output will be as shown on the terminal.
|
Let us look at sorting of a hash keys and values. | |
Slide | Syntax to sort keys is
sort open bracket keys percentage hashName close bracket semicolon
sort open bracket values percentage hashName close bracket semicolon |
Let us understand sorting functionality using a sample program. | |
Let me switch to sortHash dot pl on gedit.
| |
#!//usr/bin/perl
'City' => 'Pune', 'State' => 'Maharashtra', 'Country' => 'India' );
print "Key: ", $key, " ==> ", $addressHash{$key}, "\n"; }
print "Value: $value\n"; } |
Here we have declared hash of address.
|
Save the file and switch to terminal. | |
Terminal
perl sortHash.pl |
Execute the script by typing
perl sortHash dot pl |
Key: City ==> Pune
Key: Country ==> India Key: State ==> Maharashtra Value: India Value: Maharashtra Value: Pune |
The output will be as shown on the terminal. |
Now, let us see how to get all keys and values of hash. | |
Slide | Perl provides inbuilt function to fetch all the hash keys and values.
|
Gedit | Let us understand these functions using a sample program.
|
Gedit
'Name' => 'John', 'Department' => 'Finance' );
print "Keys of a Hash are: @keysOfHash\n";
print "Values of a Hash are: @valuesOfHash\n";
print "key: $key and value: $value\n"; } |
Type the following piece of code as shown on the screen;
|
Highlight comma in print statement
|
Let us understand the code now.
|
Press Ctrl+S to save the file. | |
Terminal
perl perlHash.pl
|
Now, let us execute the script on the terminal by typing
perl perlHash dot pl Press Enter |
Value of Key Name is: John
Keys of a Hash are: Department Name Values of a Hash are: Finance John key: Department and value: Finance key: Name and value: John |
The following output will be seen on the terminal. |
Now let us see few other ways of looping over Hash. | |
Slide
# Action to be performed } |
We can use foreach loop to iterate over each key of Hash.
|
On the Slide, point to each of the words as per the narration.
|
The syntax is as displayed on the screen.
|
foreach $variable (values(%hashName)) {
# Action to be performed } |
Similarly, we can loop over hash values as shown on the screen. |
Gedit | We will look at a sample program. So, let me switch to loopingOverHash dot pl in gedit. |
#!/usr/bin/perl
'Name' => 'John', 'Department' => 'Finance' );
print "Key is $key and it's value is $hash{$key}\n"; }
print "Value is $value\n"; } |
Type the following piece of code as shown in your loopingOverHash dot pl file.
|
Highlight
|
This piece of code returns single key of hash.
1st time dollar key ($key) contains the Department as key.
|
Switch back to the slides | Note: Hash is an unordered collection of data.
So, keys returned will not be in the sequence defined at the time of creating hash. |
Back to gedit
foreach $value (values(%hash)) |
The loop on values works in a similar way. |
Press ctrl + s | Press Ctrl + S to save the file. |
Switch to terminal
|
Then, switch to the terminal and execute the Perl script as
|
Highlight the output on the terminal
Key is Name and it's value is John ====================================== Value is Finance Value is John |
The following output is displayed on the terminal.
|
Slide: Summary | Let us summarize.
In this tutorial, we learnt -
|
Slide: Assignment | Here is assignment for you -
|
About the Project | Watch the video available at the following link
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|