Difference between revisions of "PERL/C2/Hash-in-Perl/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!  <center>'''Time'''</center>
+
|'''Time'''
!  <center>'''Narration'''</center>
+
|'''Narration'''
 
   
 
   
 
|-
 
|-
| 00.01
+
| 00:01
|Welcome to the spoken tutorial on ''' Hash''' in '''Perl'''.
+
|Welcome to the '''spoken tutorial''' on '''Hash''' in '''PERL'''.
  
 
|-
 
|-
| 00.05
+
| 00:05
| In this tutorial, we will learn about;
+
| In this tutorial, we will learn about:
  
 
|-
 
|-
|00.09
+
|00:09
|'''Hash''' in '''Perl''' and  
+
|'''Hash''' in PERL and  
 
   
 
   
 
|-
 
|-
|00.11
+
|00:11
| Accessing element of a '''hash'''  
+
| accessing an '''element''' of a '''hash'''.
 
    
 
    
 
|-
 
|-
|00.14
+
|00:14
|For this tutorial, I am using  
+
|For this tutorial, I am using:
 
   
 
   
 
|-
 
|-
| 00.16
+
| 00:16
| ''' Ubuntu Linux12.04''' operating system  
+
| '''Ubuntu Linux 12.04''' operating system  
  
 
|-
 
|-
| 00.21
+
| 00:21
|''' Perl 5.14.2''' and  
+
| '''Perl 5.14.2''' and  
  
 
|-
 
|-
| 00.24
+
| 00:24
| ''' gedit''' Text Editor  
+
| ''' gedit''' Text Editor.
  
 
|-
 
|-
|00.26
+
|00:26
 
|You can use any text editor of your choice.  
 
|You can use any text editor of your choice.  
 
   
 
   
 
|-
 
|-
|00.30
+
|00:30
|To practise this tutorial, you should have knowledge of ''' Variables & Data Structures''' in '''Perl.'''
+
|To practice this tutorial, you should have knowledge of '''variables''' & '''data Structures''' in PERL.
 
   
 
   
 
|-
 
|-
| 00.38
+
| 00:38
| 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.  
 
   
 
   
 
|-
 
|-
|00.46
+
|00:46
|Please go through the relevant spoken tutorials on the ''' spoken tutorial''' website.  
+
|Please go through the relevant spoken tutorials on the '''Spoken Tutorial''' website.  
  
 
|-
 
|-
|00.52
+
|00:52
|''' Hash''' is an unordered collection of data   
+
|'''Hash''' is an unordered collection of data.  
  
 
|-
 
|-
|00.56
+
|00:56
|It's a ''' key/value pair data structure '''
+
|It's a '''key/value''' pair '''data structure'''.
  
 
|-
 
|-
| 00.59
+
| 00:59
| '''Hash keys''' are unique  
+
| '''Hash''' keys are unique.
  
 
|-
 
|-
|01.01
+
|01:01
|However, ''' Hash''' can have duplicate values  
+
|However, '''Hash''' can have duplicate values.
  
 
|-
 
|-
|01.05
+
|01:05
| This is the declaration of a ''' hash''' .  
+
| This is the declaration of a '''hash'''.  
 
   
 
   
 
|-
 
|-
|01.08
+
|01:08
|Let us see how to get the value of a ''' key''' from '''hash. '''
+
|Let us see how to get the value of a ''' key''' from '''hash.'''
  
 
|-
 
|-
|01.12
+
|01:12
|The syntax for accessing the value of a ''' key''' is  
+
|The syntax for accessing the value of a ''' key''' is:
 
   
 
   
 
|-
 
|-
|01.17
+
|01:17
|''' dollar hashName open curly bracket single quote keyName single quote close curly bracket'''
+
|'''dollar hashName open curly bracket single quote keyName single quote close curly bracket'''.
 
   
 
   
 
|-
 
|-
|01.26
+
|01:26
 
| Let us understand ''' hash''' using a sample program.  
 
| Let us understand ''' hash''' using a sample program.  
 
   
 
   
 
|-
 
|-
| 01.31
+
| 01:31
|I have already typed the code in ''' perlHash dot pl''' file in '''gedit. '''
+
|I have already typed the code in '''perlHash dot pl''' file in '''gedit.'''
  
 
|-
 
|-
|01.37
+
|01:37
 
|Type the code as shown in your ''' perlHash dot pl''' file.  
 
|Type the code as shown in your ''' perlHash dot pl''' file.  
 
   
 
   
 
|-
 
|-
|01.42
+
|01:42
| ''' Hash''' in ''' Perl''' is declared with '''percentage''' sign.  
+
| '''Hash''' in PERL is declared with '''percentage''' sign (%).  
  
 
|-
 
|-
| 01.47
+
| 01:47
| These are the keys of '''hash.'''
+
| These are the keys of '''hash'''
 
   
 
   
 
|-
 
|-
|01.49
+
|01:49
|And these are the values of '''hash.'''   
+
|and these are the values of '''hash.'''   
 
    
 
    
 
|-
 
|-
|01.53
+
|01:53
|Note: To access ''' key''' of '''hash''', one has to use '''dollar''' sign.  
+
|Note: To access '''key''' of a '''hash''', one has to use dollar sign.  
 
   
 
   
 
|-
 
|-
|01.59
+
|01:59
|Press ''' Ctrl + S''' to save the file.  
+
|Press '''Ctrl + S''' to '''save''' the file.  
 
   
 
   
 
|-
 
|-
|02.02
+
|02:02
|Then switch to ''' terminal''' and execute the '''Perl script''' as  
+
|Then switch to '''terminal''' and execute the '''Perl script''' as:
 
   
 
   
 
|-
 
|-
|02.08
+
|02:08
|''' perl perlHash dot pl'''
+
|'''perl perlHash dot pl'''
  
 
|-
 
|-
|02.11
+
|02:11
|and press ''' Enter'''.
+
|and press '''Enter'''.
  
 
|-
 
|-
|02.14
+
|02:14
 
| The output is as shown on the terminal.  
 
| The output is as shown on the terminal.  
  
 
|-
 
|-
|02.19
+
|02:19
|Now let us see add and delete of ''' keys''' from  '''hash.'''  
+
|Now, let us see add and delete of '''keys''' from  '''hash.'''  
 
   
 
   
 
|-
 
|-
|02.24
+
|02:24
|The syntax for .
+
|The '''syntax''' for
  
 
|-
 
|-
|02.26
+
|02:26
|adding key is ''' dollar hashName open curly bracket '''
+
| Adding key is- '''dollar hashName open curly bracket'''
  
 
|-
 
|-
|02.30
+
|02:30
|''' single quote KeyName single quote'''  
+
|'''single quote KeyName single quote'''  
  
 
|-
 
|-
|02.34
+
|02:34
|''' close curly bracket equal to $value semicolon'''  
+
|'''close curly bracket equal to $value semicolon'''.
 
   
 
   
 
|-
 
|-
|02.40
+
|02:40
|deleting key is ''' delete dollar hashName open curly bracket'''  
+
| Deleting key is- '''delete dollar hashName open curly bracket'''  
  
 
|-
 
|-
|02.46
+
|02:46
|'''single quote KeyName single quote close curly bracket semicolon'''  
+
|'''single quote KeyName single quote close curly bracket semicolon'''.
 
   
 
   
 
|-
 
|-
|  02.53
+
|  02:53
 
| Now, let us understand this using a sample program.  
 
| Now, let us understand this using a sample program.  
 
   
 
   
 
|-
 
|-
|  02.58
+
|  02:58
 
| I have already typed the code in ''' hashKeyOperations dot pl''' file.  
 
| I have already typed the code in ''' hashKeyOperations dot pl''' file.  
 
   
 
   
 
|-
 
|-
| 03.05
+
| 03:05
| This is the declaration of ''' hash.'''
+
| This is the declaration of a '''hash.'''
 
   
 
   
 
|-
 
|-
|03.08
+
|03:08
|We'll be adding, deleting the ''' keys''' from this ''' hash.'''
+
|We will be adding, deleting the ''' keys''' from this ''' hash.'''
 
   
 
   
 
|-
 
|-
| 03.13
+
| 03:13
 
| Here we are adding a '''key''' to an already created '''hash.'''
 
| Here we are adding a '''key''' to an already created '''hash.'''
 
   
 
   
 
|-
 
|-
| 03.18
+
| 03:18
|It is like assigning a value to a variable.  
+
|It is like assigning a value to a '''variable'''.  
  
 
|-
 
|-
|03.23
+
|03:23
|''' delete''' keyword is used to delete the ''' key.'''  
+
|'''delete''' keyword is used to delete the '''key.'''  
 
   
 
   
 
|-
 
|-
|03.27
+
|03:27
 
|We need to pass the '''key''' to delete it.  
 
|We need to pass the '''key''' to delete it.  
 
   
 
   
 
|-
 
|-
| 03.31
+
| 03:31
| Press ''' Ctrl+S''' to save the file.  
+
| Press ''' Ctrl+S''' to '''save''' the file.  
 
   
 
   
 
|-
 
|-
|03.35
+
|03:35
|Switch to the terminal and execute the ''' Perl script''' as  
+
|Switch to the '''terminal''' and '''execute''' the ''' Perl script''' as:
 
   
 
   
 
|-
 
|-
| 03.40
+
| 03:40
 
| ''' perl hashKeyOperations dot pl'''  
 
| ''' perl hashKeyOperations dot pl'''  
 
   
 
   
 
|-
 
|-
|   03.44
+
| 03:44
|  and press ''' Enter.'''
+
|  and press '''Enter.'''
 
   
 
   
 
|-
 
|-
|  03.47
+
|  03:47
 
| Output will be as shown on the terminal.
 
| Output will be as shown on the terminal.
 
   
 
   
 
|-
 
|-
| 03.52
+
| 03:52
| Let us look at ''' sorting''' of a ''' hash keys''' and '''values.'''
+
| Let us look at sorting of '''hash keys''' and '''values.'''
  
 
|-
 
|-
| 03.57
+
| 03:57
| Syntax to ''' sort keys''' is  
+
| Syntax to '''sort''' '''keys''' is:
  
 
|-
 
|-
|04.00
+
|04:00
|''' sort open bracket keys percentage hashName close bracket semicolon '''
+
|'''sort open bracket keys percentage hashName close bracket semicolon'''.
 
   
 
   
 
|-
 
|-
|04.07
+
|04:07
|Similarly, we can '''sort hash values''' as  
+
|Similarly, we can sort '''hash values''' as:
  
 
|-
 
|-
|04.11
+
|04:11
|''' sort open bracket values percentage hashName close bracket semicolon'''  
+
|'''sort open bracket values percentage hashName close bracket semicolon'''.
 
   
 
   
 
|-
 
|-
|04.18
+
|04:18
|Let us understand ''' sorting''' functionality using a sample program.   
+
|Let us understand sorting functionality using a sample program.   
  
 
|-
 
|-
|04.24
+
|04:24
|Let me switch to ''' sortHash dot pl'''  on ''' gedit.'''
+
|Let me switch to ''' sortHash dot pl'''  on '''gedit.'''
 
   
 
   
 
|-
 
|-
| 04.30
+
| 04:30
|Type the code as displayed on the screen in your ''' sortHash dot pl''' file.  
+
|Type the code as displayed on the screen, in your '''sortHash dot pl''' file.  
  
 
|-
 
|-
| 04.36
+
| 04:36
| Here we have declared ''' hash''' of '''address. '''
+
| Here, we have declared '''hash''' of '''address.'''
 
   
 
   
 
|-
 
|-
|04.41
+
|04:41
|Here, to sort the ''' keys''' we have used the '''sort''' inbuilt function, along with the '''keys''' function.  
+
|Here, to sort the '''keys''', we have used the '''sort''' inbuilt function along with the '''keys''' function.  
  
 
|-
 
|-
|04.49
+
|04:49
 
|This will sort the '''hash keys''' in alphabetical order.  
 
|This will sort the '''hash keys''' in alphabetical order.  
 
   
 
   
 
|-
 
|-
|04.54
+
|04:54
 
|Similarly, we can use the '''sort''' function on '''values''' of '''hash.'''  
 
|Similarly, we can use the '''sort''' function on '''values''' of '''hash.'''  
  
 
|-
 
|-
|04.59
+
|04:59
|Sorting can also be done on ''' numeric keys''' and/or '''values.'''
+
|Sorting can also be done on numeric keys and/or values.  
 
   
 
   
 
|-
 
|-
|05.05
+
|05:05
|Save the file and switch to terminal.  
+
|'''Save''' the file and switch to terminal.  
  
 
|-
 
|-
|05.09
+
|05:09
|Execute the script by typing ''' perl sortHash dot pl''' and Press ''' Enter '''
+
|Execute the script by typing ''' perl sortHash dot pl''' and press '''Enter'''.
  
 
|-
 
|-
|05.17
+
|05:17
 
|The output will be as shown on the terminal.
 
|The output will be as shown on the terminal.
 
   
 
   
 
|-
 
|-
|05.22
+
|05:22
|Now, let us see how to get all ''' keys ''' and ''' values''' of '''hash.'''
+
|Now, let us see how to get all '''keys''' and '''values''' of a '''hash.'''
  
 
|-
 
|-
|  05.27
+
|  05:27
|''' Perl''' provides inbuilt ''' function''' to fetch all the ''' hash keys''' and ''' values.'''  
+
|PERL provides inbuilt function to fetch all the '''hash keys''' and '''values.'''  
 
   
 
   
 
|-
 
|-
|  05.34
+
|  05:34
|''' keys function''' is used to retrive all the ''' keys''' of ''' hash '''
+
|'''keys()''' function is used to retrieve all the keys of '''hash''',
 
   
 
   
 
|-
 
|-
|05.40
+
|05:40
| ''' values''' function returns ''' values''' of all the ''' keys''' whereas  
+
| '''values()''' function returns '''values''' of all the '''keys''' whereas  
 
   
 
   
 
|-
 
|-
|  05.46
+
|  05:46
| ''' each''' function iterates over ''' hash''' and returns ''' key/value''' pair from ''' hash'''  
+
| '''each()''' function iterates over ''' hash''' and returns '''key/value''' pair from ''' hash'''.
  
 
|-
 
|-
|  05.53
+
|  05:53
| Let us understand these using a sample program.  
+
| Let us understand these using a sample program.  
 
   
 
   
 
|-
 
|-
| 05.57
+
| 05:57
| For this, we'll use '''perlHash dot pl''' script, which we have created earlier in this tutorial.  
+
| For this, we'll use '''perlHash dot pl''' script which we have created earlier in this tutorial.  
  
 
|-
 
|-
|  06.07
+
|  06:07
| Type the following piece of code as shown on the screen;
+
| Type the following piece of code as shown on the screen.
  
 
|-
 
|-
|  06.12
+
|  06:12
 
| Let us understand the code now.  
 
| Let us understand the code now.  
  
 
|-
 
|-
|06.15
+
|06:15
|''' keys''' function on ''' hash''', returns an '''array''' which contains all '''keys''' of '''hash.'''
+
|'''keys()''' function on hash returns an array which contains all '''keys''' of '''hash.'''
 
   
 
   
 
|-
 
|-
|  06.22
+
|  06:22
| ''' values''' function on ''' hash''' returns an '''array''' of '''values''' for all '''keys''' of '''hash'''.
+
| '''values()''' function on hash returns an array of values for all '''keys''' of hash.
 
   
 
   
 
|-
 
|-
|  06.30
+
|  06:30
| ''' each'''  function returns the ''' key/value''' pair.  
+
| '''each()'''  function returns the ''' key/value''' pair.  
  
 
|-
 
|-
|06.34
+
|06:34
|Here, we have used the ''' while loop.'''
+
|Here, we have used the '''while''' loop.
  
 
|-
 
|-
| 06.36
+
| 06:36
|It will iterate over each ''' key/value''' pair of ''' hash,''' that is returned by '''each function'''.
+
|It will iterate over each '''key/value''' pair of hash that is returned by '''each''' function.
  
 
|-
 
|-
|06.43
+
|06:43
| Press ''' Ctrl+S''' to save the file.  
+
| Press '''Ctrl+S''' to '''save''' the file.  
  
 
|-
 
|-
|06.48
+
|06:48
 
|Now, let us execute the script on the terminal by typing  
 
|Now, let us execute the script on the terminal by typing  
 
   
 
   
 
|-
 
|-
| 06.53
+
| 06:53
| ''' perl perlHash dot pl'''  
+
| ''''perl perlHash dot pl''''  
  
 
|-
 
|-
| 06.58
+
| 06:58
| And Press ''' Enter'''
+
| and press '''Enter'''.
  
 
|-
 
|-
|  07.01
+
|  07:01
 
| The following output will be seen on the terminal.
 
| The following output will be seen on the terminal.
 
   
 
   
 
|-
 
|-
|  07.05
+
|  07:05
|Now let us see few other ways of looping over ''' hash.'''
+
|Now, let us see few other ways of looping over hash.
 
   
 
   
 
|-
 
|-
|07.10
+
|07:10
| We can use ''' foreach loop''' to iterate over each '''key''' of '''hash'''.  
+
| We can use '''foreach''' loop to iterate over each '''key''' of hash.  
 
   
 
   
 
|-
 
|-
|07.15
+
|07:15
|Then perform a set of actions on the value of a key.
+
|Then perform a set of actions on the value of key.
 
   
 
   
 
|-
 
|-
|07.20
+
|07:20
 
|The syntax is as displayed on the screen.  
 
|The syntax is as displayed on the screen.  
 
 
 
 
 
|-
 
|-
|07.24
+
|07:24
| Here, each iteration of ''' foreach loop''' will assign '''key''' from ''' hash''' to ''' $variable.'''
+
| Here, each iteration of '''foreach''' loop will assign '''key''' from hash to ''' $variable.'''
  
 
|-
 
|-
|07.32
+
|07:32
|Then it will use that ''' $variable''' to fetch the  value or to perform a set of actions.  
+
|Then, it will use that '''$variable''' to fetch the  '''value''' or to perform a set of actions.  
  
 
|-
 
|-
|07.40
+
|07:40
 
| Similarly, we can loop over '''hash values''' as shown on the screen.  
 
| Similarly, we can loop over '''hash values''' as shown on the screen.  
  
 
|-
 
|-
|07.47
+
|07:47
 
|We will look at sample program.  
 
|We will look at sample program.  
 
   
 
   
 
|-
 
|-
|07.49
+
|07:49
|So, let me switch to ''' loopingOverHash dot pl''' in '''gedit.'''
+
|So, let me switch to '''loopingOverHash dot pl''' in '''gedit.'''
  
 
|-
 
|-
|07.55
+
|07:55
| Type the following piece of code as shown in your ''' loopingOverHash dot pl'''  
+
| Type the following piece of code as shown in your '''loopingOverHash dot pl'''.
  
 
|-
 
|-
|08.02
+
|08:02
|This piece of code returns single ''' key''' of '''hash.'''
+
|This piece of code returns single '''key''' of hash.
  
 
|-
 
|-
|08.07
+
|08:07
|Here in our case,  
+
|Here, in our case,  
  
 
|-
 
|-
|08.09
+
|08:09
|1st time ''' dollar key ($key)''' contains the ''' Department''' as ''' key.'''  
+
|1st time '''dollar key ($key)''' contains the '''Department''' as ''' key.'''  
  
 
|-
 
|-
|08.15
+
|08:15
|In the next iteration of '''foreach, Name key''' is returned.  
+
|In the next iteration of '''foreach''', '''Name''' key is returned.  
  
 
|-
 
|-
|08.21
+
|08:21
|Note: ''' Hash''' is an unordered collection of data.  
+
|Note: '''Hash''' is an unordered collection of data.  
  
 
|-
 
|-
|08.26
+
|08:26
|So, ''' keys''' returned will not be in the sequence defined at the time of creating '''hash.'''
+
|So, '''keys''' returned will not be in the sequence defined at the time of creating hash.  
  
 
|-
 
|-
|08.33
+
|08:33
| The loop on ''' values''' works in a similar way.  
+
| The '''loop''' on '''values''' works in a similar way.  
  
 
|-
 
|-
|08.38
+
|08:38
|Press ''' Ctrl + S''' to save the file.  
+
|Press '''Ctrl + S''' to '''save''' the file.  
  
 
|-
 
|-
|08.41
+
|08:41
|Then, switch to terminal and execute the ''' Perl script''' as  
+
|Then, switch to terminal and execute the ''' Perl script''' as:
  
 
|-
 
|-
|08.46
+
|08:46
 
|''' perl loopingOverHash dot pl'''  
 
|''' perl loopingOverHash dot pl'''  
  
 
|-
 
|-
|08.50
+
|08:50
|and press ''' Enter.'''
+
|and press '''Enter.'''
  
 
|-
 
|-
|08.53
+
|08:53
 
|The following output is displayed on the terminal.
 
|The following output is displayed on the terminal.
  
 
|-
 
|-
|08.58
+
|08:58
|Let us summarize.  
+
|Let us summarize. In this tutorial, we learnt: 
  
 
|-
 
|-
|08.59
+
|09:01
|In this tutorial, we learnt -
+
|'''Hash''' in PERL and
  
 
|-
 
|-
|09.01
+
|09:03
|''' Hash''' in '''Perl''' and
+
|accessing elements of a '''hash'''  
  
 
|-
 
|-
|09.03
+
|09:05
|Accessing elements of a '''hash'''
+
 
+
|-
+
|09.05
+
 
|using sample programs.  
 
|using sample programs.  
  
 
|-
 
|-
|09.08
+
|09:08
|Here is assignment for you -
+
|Here is an assignment for you
  
 
|-
 
|-
|09.11
+
|09:11
 
|Declare '''hash''' having '''student name''' as''' key'''  
 
|Declare '''hash''' having '''student name''' as''' key'''  
  
 
|-
 
|-
|09.15
+
|09:15
|And '''his/her percentage''' as the '''value'''.  
+
|and his/her percentage as the '''value'''.  
  
 
|-
 
|-
|09.18
+
|09:18
|Loop over '''hash''' using '''keys, values''' and '''each''' function  
+
|Loop over '''hash''' using '''keys(), values()''' and '''each()''' function.
  
 
|-
 
|-
|09.24
+
|09:24
|Then print the ''' percentage''' of each student.  
+
|Then print the percentage of each student.  
  
 
|-
 
|-
|09.29
+
|09:29
|Watch the video available at the following link
+
|Watch the video available at the following link.
 
   
 
   
 
|-
 
|-
|09.32
+
|09:32
|It summaries the Spoken Tutorial project
+
|It summarizes the Spoken Tutorial project.
 
   
 
   
 
|-
 
|-
|09.37
+
|09:37
|If you do not have good bandwidth, you can download and watch it
+
|If you do not have good bandwidth, you can download and watch it.
 
   
 
   
 
|-
 
|-
| 09.42
+
| 09:42
|  The Spoken Tutorial Project Team  Conducts workshops using spoken tutorials
+
|  The Spoken Tutorial Project team: Conducts workshops using spoken tutorials.
 
   
 
   
 
|-
 
|-
|09.49
+
|09:49
|Gives certificates to those who pass an online test
+
|Gives certificates to those who pass an online test.
 
   
 
   
 
|-
 
|-
|09.53
+
|09:53
|For more details, please write to contact at spoken hyphen tutorial dot org
+
|For more details, please write to: contact at spoken hyphen tutorial dot org.
 
   
 
   
 
|-
 
|-
| 10.02
+
| 10:02
|  Spoken Tutorial Project is a part of the Talk to a  Teacher project
+
'''Spoken Tutorial''' project is a part of the "Talk to a  Teacher" project.
 
   
 
   
 
|-
 
|-
|10.06
+
|10:06
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
   
 
   
 
|-
 
|-
|10.15
+
|10:15
|More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
|More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
 
   
 
   
 
|-
 
|-
| 10.26
+
| 10:26
| Hope you enjoyed this Perl tutorial.  
+
| Hope you enjoyed this '''PERL''' tutorial.  
 
   
 
   
 
|-
 
|-
|10.30
+
|10:30
|This is Amol signing off.
+
|This is Amol, signing off.
 
   
 
   
 
|-
 
|-
|10.33
+
|10:33
 
|Thanks for joining.
 
|Thanks for joining.
 
   
 
   
 
|}
 
|}

Latest revision as of 15:49, 10 March 2017

Time Narration
00:01 Welcome to the spoken tutorial on Hash in PERL.
00:05 In this tutorial, we will learn about:
00:09 Hash in PERL and
00:11 accessing an element of a hash.
00:14 For this tutorial, I am using:
00:16 Ubuntu Linux 12.04 operating system
00:21 Perl 5.14.2 and
00:24 gedit Text Editor.
00:26 You can use any text editor of your choice.
00:30 To practice this tutorial, you should have knowledge of variables & data Structures in PERL.
00:38 Knowledge of comments, loops, conditional statements and arrays will be an added advantage.
00:46 Please go through the relevant spoken tutorials on the Spoken Tutorial website.
00:52 Hash is an unordered collection of data.
00:56 It's a key/value pair data structure.
00:59 Hash keys are unique.
01:01 However, Hash can have duplicate values.
01:05 This is the declaration of a hash.
01:08 Let us see how to get the value of a key from hash.
01:12 The syntax for accessing the value of a key is:
01:17 dollar hashName open curly bracket single quote keyName single quote close curly bracket.
01:26 Let us understand hash using a sample program.
01:31 I have already typed the code in perlHash dot pl file in gedit.
01:37 Type the code as shown in your perlHash dot pl file.
01:42 Hash in PERL is declared with percentage sign (%).
01:47 These are the keys of hash
01:49 and these are the values of hash.
01:53 Note: To access key of a hash, one has to use dollar sign.
01:59 Press Ctrl + S to save the file.
02:02 Then switch to terminal and execute the Perl script as:
02:08 perl perlHash dot pl
02:11 and press Enter.
02:14 The output is as shown on the terminal.
02:19 Now, let us see add and delete of keys from hash.
02:24 The syntax for:
02:26 Adding key is- dollar hashName open curly bracket
02:30 single quote KeyName single quote
02:34 close curly bracket equal to $value semicolon.
02:40 Deleting key is- delete dollar hashName open curly bracket
02:46 single quote KeyName single quote close curly bracket semicolon.
02:53 Now, let us understand this using a sample program.
02:58 I have already typed the code in hashKeyOperations dot pl file.
03:05 This is the declaration of a hash.
03:08 We will be adding, deleting the keys from this hash.
03:13 Here we are adding a key to an already created hash.
03:18 It is like assigning a value to a variable.
03:23 delete keyword is used to delete the key.
03:27 We need to pass the key to delete it.
03:31 Press Ctrl+S to save the file.
03:35 Switch to the terminal and execute the Perl script as:
03:40 perl hashKeyOperations dot pl
03:44 and press Enter.
03:47 Output will be as shown on the terminal.
03:52 Let us look at sorting of hash keys and values.
03:57 Syntax to sort keys is:
04:00 sort open bracket keys percentage hashName close bracket semicolon.
04:07 Similarly, we can sort hash values as:
04:11 sort open bracket values percentage hashName close bracket semicolon.
04:18 Let us understand sorting functionality using a sample program.
04:24 Let me switch to sortHash dot pl on gedit.
04:30 Type the code as displayed on the screen, in your sortHash dot pl file.
04:36 Here, we have declared hash of address.
04:41 Here, to sort the keys, we have used the sort inbuilt function along with the keys function.
04:49 This will sort the hash keys in alphabetical order.
04:54 Similarly, we can use the sort function on values of hash.
04:59 Sorting can also be done on numeric keys and/or values.
05:05 Save the file and switch to terminal.
05:09 Execute the script by typing perl sortHash dot pl and press Enter.
05:17 The output will be as shown on the terminal.
05:22 Now, let us see how to get all keys and values of a hash.
05:27 PERL provides inbuilt function to fetch all the hash keys and values.
05:34 keys() function is used to retrieve all the keys of a hash,
05:40 values() function returns values of all the keys whereas
05:46 each() function iterates over hash and returns key/value pair from hash.
05:53 Let us understand these using a sample program.
05:57 For this, we'll use perlHash dot pl script which we have created earlier in this tutorial.
06:07 Type the following piece of code as shown on the screen.
06:12 Let us understand the code now.
06:15 keys() function on hash returns an array which contains all keys of hash.
06:22 values() function on hash returns an array of values for all keys of hash.
06:30 each() function returns the key/value pair.
06:34 Here, we have used the while loop.
06:36 It will iterate over each key/value pair of hash that is returned by each function.
06:43 Press Ctrl+S to save the file.
06:48 Now, let us execute the script on the terminal by typing
06:53 'perl perlHash dot pl'
06:58 and press Enter.
07:01 The following output will be seen on the terminal.
07:05 Now, let us see few other ways of looping over hash.
07:10 We can use foreach loop to iterate over each key of hash.
07:15 Then perform a set of actions on the value of key.
07:20 The syntax is as displayed on the screen.
07:24 Here, each iteration of foreach loop will assign key from hash to $variable.
07:32 Then, it will use that $variable to fetch the value or to perform a set of actions.
07:40 Similarly, we can loop over hash values as shown on the screen.
07:47 We will look at sample program.
07:49 So, let me switch to loopingOverHash dot pl in gedit.
07:55 Type the following piece of code as shown in your loopingOverHash dot pl.
08:02 This piece of code returns single key of hash.
08:07 Here, in our case,
08:09 1st time dollar key ($key) contains the Department as key.
08:15 In the next iteration of foreach, Name key is returned.
08:21 Note: Hash is an unordered collection of data.
08:26 So, keys returned will not be in the sequence defined at the time of creating hash.
08:33 The loop on values works in a similar way.
08:38 Press Ctrl + S to save the file.
08:41 Then, switch to terminal and execute the Perl script as:
08:46 perl loopingOverHash dot pl
08:50 and press Enter.
08:53 The following output is displayed on the terminal.
08:58 Let us summarize. In this tutorial, we learnt:
09:01 Hash in PERL and
09:03 accessing elements of a hash
09:05 using sample programs.
09:08 Here is an assignment for you:
09:11 Declare hash having student name as key
09:15 and his/her percentage as the value.
09:18 Loop over hash using keys(), values() and each() function.
09:24 Then print the percentage of each student.
09:29 Watch the video available at the following link.
09:32 It summarizes the Spoken Tutorial project.
09:37 If you do not have good bandwidth, you can download and watch it.
09:42 The Spoken Tutorial Project team: Conducts workshops using spoken tutorials.
09:49 Gives certificates to those who pass an online test.
09:53 For more details, please write to: contact at spoken hyphen tutorial dot org.
10:02 Spoken Tutorial project is a part of the "Talk to a Teacher" project.
10:06 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:15 More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro.
10:26 Hope you enjoyed this PERL tutorial.
10:30 This is Amol, signing off.
10:33 Thanks for joining.

Contributors and Content Editors

Gaurav, Nancyvarkey, PoojaMoolya, Sandhya.np14