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 | This is the declaration of a hash . |
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. |
#!/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.
delete keyword is used to delete the key. 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
|
Highlight the output. | Output will be as shown on the terminal.
<<pause>> |
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.
Similarly, we can use the sort function on values of hash.
|
Save the file and switch to terminal. | |
Terminal
perl sortHash.pl |
Execute the script by typing
perl sortHash dot pl |
Highlight the output | 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 |
Highlight the output. | 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.
|
#!/usr/bin/perl
'Name' => 'John', 'Department' => 'Finance' );
print "Key is $key and it's value is $hash{$key}\n"; }
====================\n";
print "Value is $value\n"; } |
Type the following piece of code as shown in your loopingOverHash dot pl
|
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 terminal and execute the Perl script as
perl loopingOverHash dot pl
|
Highlight the output on the terminal | The following output is displayed on the terminal.
<pause> |
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.
|