Difference between revisions of "PERL/C2/Hash-in-Perl/English"
(Created page with ''''Title Of Script: Hash in Perl''' '''Author:''' Amol Brahmankar '''Keywords: '''Hash in perl, Associative Array in Perl video tutorial. {| style="border-spacing:0;" | styl…') |
Nancyvarkey (Talk | contribs) |
||
Line 21: | Line 21: | ||
* '''Hash''' in '''Perl '''and | * '''Hash''' in '''Perl '''and | ||
* Accessing element of a '''hash''' | * Accessing element of a '''hash''' | ||
− | |||
− | |||
|- | |- | ||
Line 39: | Line 37: | ||
− | Knowledge of''' comments, loops, conditional statements '''and '''Arrays''' will be an added advantage | + | Knowledge of''' comments, loops, conditional statements '''and '''Arrays''' will be an added advantage. |
Line 50: | Line 48: | ||
* '''Hash keys''' are unique | * '''Hash keys''' are unique | ||
* However, '''Hash''' can have duplicate values | * However, '''Hash''' can have duplicate values | ||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This is the declaration of a '''hash '''. |
|- | |- | ||
Line 99: | Line 95: | ||
<nowiki><<pause>></nowiki> | <nowiki><<pause>></nowiki> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
Line 147: | Line 148: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal | ||
− | |||
− | |||
− | |||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output is as shown on '''terminal'''<nowiki>. <pause></nowiki> | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output is as shown on '''terminal'''<nowiki>. <pause></nowiki> | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 167: | Line 162: | ||
'''dollar hashName open curly bracket''' | '''dollar hashName open curly bracket''' | ||
− | '''single quote KeyName single quote | + | '''single quote KeyName single quote ''' |
− | '''curly bracket equal to value semicolon''' | + | '''close curly bracket equal to value semicolon''' |
2. deleting key is | 2. deleting key is | ||
Line 175: | Line 170: | ||
'''delete dollar hashName open curly bracket''' | '''delete dollar hashName open curly bracket''' | ||
− | '''single quote KeyName single quote | + | '''single quote KeyName single quote ''' |
− | '''curly bracket semicolon''' | + | '''close curly bracket semicolon''' |
|- | |- | ||
Line 185: | Line 180: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal hashKeyOperations dot pl | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal hashKeyOperations dot pl | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I have already typed the code in '''hashKeyOperations dot pl''' file | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I have already typed the code in '''hashKeyOperations dot pl''' file. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 244: | Line 233: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ' | + | '''delete''' keyword is used to delete the '''key'''. |
− | ' | + | We need to pass the '''key''' to delete it. |
− | |||
− | + | |- | |
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| |
+ | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Press '''Ctrl+S''' to save the file. | ||
+ | |- | ||
+ | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal | ||
+ | |||
+ | '''perl hashKeyOperations.pl ''' | ||
+ | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Switch to the '''terminal '''and execute the '''Perl script '''as | ||
+ | |||
+ | |||
+ | '''perl hashKeyOperations dot pl ''' | ||
+ | |||
+ | |||
+ | and press '''Enter.''' | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output. | ||
+ | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Output will be as shown on the '''terminal.''' | ||
<nowiki><<pause>></nowiki> | <nowiki><<pause>></nowiki> | ||
Line 364: | Line 343: | ||
This will sort the '''hash keys '''in alphabetical order. | This will sort the '''hash keys '''in alphabetical order. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
Line 384: | Line 374: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output will be as shown on the '''terminal.''' | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output will be as shown on the '''terminal.''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us see how to get all '''keys''' and '''values''' of '''hash.''' |
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Perl''' provides inbuilt | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Perl''' provides inbuilt '''function''' to fetch all the '''hash keys''' and '''values'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | * '''keys function''' is used to retrive all the '''keys''' of '''hash''' | ||
+ | * '''values''' function returns '''values '''of all the '''keys''' whereas | ||
+ | * '''each''' function iterates over '''hash '''and returns '''key/value '''pair from '''hash''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand these functions using a sample program. | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand these '''functions''' using a sample program. |
− | For this, we'll use '''perlHash dot pl''' script, which we created earlier in this tutorial. | + | For this, we'll use '''perlHash dot pl''' script, which we have created earlier in this tutorial. |
|- | |- | ||
Line 456: | Line 434: | ||
<nowiki><<pause>></nowiki> | <nowiki><<pause>></nowiki> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
Line 503: | Line 497: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output will be seen on the '''terminal.''' | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output will be seen on the '''terminal.''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us see few other ways of looping over ''' | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us see few other ways of looping over '''hash.''' |
|- | |- | ||
Line 527: | Line 513: | ||
} | } | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We can use '''foreach loop '''to iterate over each key of ''' | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We can use '''foreach loop '''to iterate over each '''key''' of '''hash'''. |
Line 555: | Line 541: | ||
|- | |- | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We will look at a sample program. So, let me switch to '''loopingOverHash dot pl''' in '''gedit.''' | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We will look at a sample program. |
+ | |||
+ | |||
+ | So, let me switch to '''loopingOverHash dot pl''' in '''gedit.''' | ||
|- | |- | ||
Line 577: | Line 566: | ||
− | '''print "======================================\n"; ''' | + | '''print "==================''' |
+ | |||
+ | '''====================\n"; ''' | ||
Line 585: | Line 576: | ||
'''} ''' | '''} ''' | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the following piece of code as shown in your '''loopingOverHash dot pl ''' | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the following piece of code as shown in your '''loopingOverHash dot pl ''' |
Line 606: | Line 597: | ||
− | In the next iteration of '''foreach, Name key '''is | + | In the next iteration of '''foreach, Name key '''is returned. |
|- | |- | ||
Line 632: | Line 623: | ||
− | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then, switch to | + | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then, switch to '''terminal '''and execute the '''Perl script '''as |
− | + | ||
'''perl loopingOverHash dot pl''' | '''perl loopingOverHash dot pl''' | ||
Line 643: | Line 633: | ||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output is displayed on the '''terminal.''' | | style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output is displayed on the '''terminal.''' | ||
− | |||
<nowiki><pause></nowiki> | <nowiki><pause></nowiki> | ||
Line 667: | Line 646: | ||
* Accessing '''elements '''of a '''hash''' | * Accessing '''elements '''of a '''hash''' | ||
* using sample programs. | * using sample programs. | ||
− | |||
− | |||
|- | |- | ||
Line 678: | Line 655: | ||
# Loop over '''hash''' using '''keys, values '''and '''each '''function | # Loop over '''hash''' using '''keys, values '''and '''each '''function | ||
# Then print the '''percentage''' of each student. | # Then print the '''percentage''' of each student. | ||
− | |||
− | |||
|- | |- |
Revision as of 12:47, 7 March 2014
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.
|