1 00:00:01 --> 00:00:04 Welcome to the spoken tutorial on Hash in Perl. 2 00:00:05 --> 00:00:08 In this tutorial, we will learn about; 3 00:00:09 --> 00:00:10 Hash in Perl and 4 00:00:11 --> 00:00:13 Accessing element of a hash 5 00:00:14 --> 00:00:15 For this tutorial, I am using 6 00:00:16 --> 00:00:20 Ubuntu Linux12.04 operating system 7 00:00:21 --> 00:00:23 Perl 5.14.2 and 8 00:00:24 --> 00:00:25 gedit Text Editor 9 00:00:26 --> 00:00:29 You can use any text editor of your choice. 10 00:00:30 --> 00:00:37 To practise this tutorial, you should have knowledge of Variables & Data Structures in Perl. 11 00:00:38 --> 00:00:45 Knowledge of comments, loops, conditional statements and Arrays will be an added advantage. 12 00:00:46 --> 00:00:51 Please go through the relevant spoken tutorials on the spoken tutorial website. 13 00:00:52 --> 00:00:55 Hash is an unordered collection of data 14 00:00:56 --> 00:00:58 It's a key/value pair data structure 15 00:00:59 --> 00:01:00 Hash keys are unique 16 00:01:01 --> 00:01:04 However, Hash can have duplicate values 17 00:01:05 --> 00:01:07 This is the declaration of a hash . 18 00:01:08 --> 00:01:11 Let us see how to get the value of a key from hash. 19 00:01:12 --> 00:01:16 The syntax for accessing the value of a key is 20 00:01:17 --> 00:01:25 dollar hashName open curly bracket single quote keyName single quote close curly bracket 21 00:01:26 --> 00:01:30 Let us understand hash using a sample program. 22 00:01:31 --> 00:01:36 I have already typed the code in perlHash dot pl file in gedit. 23 00:01:37 --> 00:01:41 Type the code as shown in your perlHash dot pl file. 24 00:01:42 --> 00:01:46 Hash in Perl is declared with percentage sign. 25 00:01:47 --> 00:01:48 These are the keys of hash. 26 00:01:49 --> 00:01:52 And these are the values of hash. 27 00:01:53 --> 00:01:58 Note: To access key of hash, one has to use dollar sign. 28 00:01:59 --> 00:02:01 Press Ctrl + S to save the file. 29 00:02:02 --> 00:02:07 Then switch to terminal and execute the Perl script as 30 00:02:08 --> 00:02:10 perl perlHash dot pl 31 00:02:11 --> 00:02:13 and press Enter. 32 00:02:14 --> 00:02:18 The output is as shown on the terminal. 33 00:02:19 --> 00:02:23 Now let us see add and delete of keys from hash. 34 00:02:24 --> 00:02:25 The syntax for . 35 00:02:26 --> 00:02:29 adding key is dollar hashName open curly bracket 36 00:02:30 --> 00:02:33 single quote KeyName single quote 37 00:02:34 --> 00:02:39 close curly bracket equal to $value semicolon 38 00:02:40 --> 00:02:45 deleting key is delete dollar hashName open curly bracket 39 00:02:46 --> 00:02:52 single quote KeyName single quote close curly bracket semicolon 40 00:02:53 --> 00:02:57 Now, let us understand this using a sample program. 41 00:02:58 --> 00:03:04 I have already typed the code in hashKeyOperations dot pl file. 42 00:03:05 --> 00:03:07 This is the declaration of hash. 43 00:03:08 --> 00:03:12 We'll be adding, deleting the keys from this hash. 44 00:03:13 --> 00:03:17 Here we are adding a key to an already created hash. 45 00:03:18 --> 00:03:22 It is like assigning a value to a variable. 46 00:03:23 --> 00:03:26 delete keyword is used to delete the key. 47 00:03:27 --> 00:03:30 We need to pass the key to delete it. 48 00:03:31 --> 00:03:34 Press Ctrl+S to save the file. 49 00:03:35 --> 00:03:39 Switch to the terminal and execute the Perl script as 50 00:03:40 --> 00:03:43 perl hashKeyOperations dot pl 51 00:03:44 --> 00:03:46 and press Enter. 52 00:03:47 --> 00:03:51 Output will be as shown on the terminal. 53 00:03:52 --> 00:03:56 Let us look at sorting of a hash keys and values. 54 00:03:57 --> 00:03:59 Syntax to sort keys is 55 00:04:00 --> 00:04:06 sort open bracket keys percentage hashName close bracket semicolon 56 00:04:07 --> 00:04:10 Similarly, we can sort hash values as 57 00:04:11 --> 00:04:17 sort open bracket values percentage hashName close bracket semicolon 58 00:04:18 --> 00:04:23 Let us understand sorting functionality using a sample program. 59 00:04:24 --> 00:04:29 Let me switch to sortHash dot pl on gedit. 60 00:04:30 --> 00:04:35 Type the code as displayed on the screen in your sortHash dot pl file. 61 00:04:36 --> 00:04:40 Here we have declared hash of address. 62 00:04:41 --> 00:04:48 Here, to sort the keys we have used the sort inbuilt function, along with the keys function. 63 00:04:49 --> 00:04:53 This will sort the hash keys in alphabetical order. 64 00:04:54 --> 00:04:58 Similarly, we can use the sort function on values of hash. 65 00:04:59 --> 00:05:04 Sorting can also be done on numeric keys and/or values. 66 00:05:05 --> 00:05:08 Save the file and switch to terminal. 67 00:05:09 --> 00:05:16 Execute the script by typing perl sortHash dot pl and Press Enter 68 00:05:17 --> 00:05:21 The output will be as shown on the terminal. 69 00:05:22 --> 00:05:26 Now, let us see how to get all keys and values of hash. 70 00:05:27 --> 00:05:33 Perl provides inbuilt function to fetch all the hash keys and values. 71 00:05:34 --> 00:05:39 keys function is used to retrive all the keys of hash 72 00:05:40 --> 00:05:45 values function returns values of all the keys whereas 73 00:05:46 --> 00:05:52 each function iterates over hash and returns key/value pair from hash 74 00:05:53 --> 00:05:56 Let us understand these using a sample program. 75 00:05:57 --> 00:06:06 For this, we'll use perlHash dot pl script, which we have created earlier in this tutorial. 76 00:06:07 --> 00:06:11 Type the following piece of code as shown on the screen; 77 00:06:12 --> 00:06:14 Let us understand the code now. 78 00:06:15 --> 00:06:21 keys function on hash, returns an array which contains all keys of hash. 79 00:06:22 --> 00:06:29 values function on hash returns an array of values for all keys of hash. 80 00:06:30 --> 00:06:33 each function returns the key/value pair. 81 00:06:34 --> 00:06:35 Here, we have used the while loop. 82 00:06:36 --> 00:06:42 It will iterate over each key/value pair of hash, that is returned by each function. 83 00:06:43 --> 00:06:47 Press Ctrl+S to save the file. 84 00:06:48 --> 00:06:52 Now, let us execute the script on the terminal by typing 85 00:06:53 --> 00:06:57 perl perlHash dot pl 86 00:06:58 --> 00:07:00 And Press Enter 87 00:07:01 --> 00:07:04 The following output will be seen on the terminal. 88 00:07:05 --> 00:07:09 Now let us see few other ways of looping over hash. 89 00:07:10 --> 00:07:14 We can use foreach loop to iterate over each key of hash. 90 00:07:15 --> 00:07:19 Then perform a set of actions on the value of a key. 91 00:07:20 --> 00:07:23 The syntax is as displayed on the screen. 92 00:07:24 --> 00:07:31 Here, each iteration of foreach loop will assign key from hash to $variable. 93 00:07:32 --> 00:07:39 Then it will use that $variable to fetch the value or to perform a set of actions. 94 00:07:40 --> 00:07:46 Similarly, we can loop over hash values as shown on the screen. 95 00:07:47 --> 00:07:48 We will look at sample program. 96 00:07:49 --> 00:07:54 So, let me switch to loopingOverHash dot pl in gedit. 97 00:07:55 --> 00:08:01 Type the following piece of code as shown in your loopingOverHash dot pl 98 00:08:02 --> 00:08:06 This piece of code returns single key of hash. 99 00:08:07 --> 00:08:08 Here in our case, 100 00:08:09 --> 00:08:14 1st time dollar key ($key) contains the Department as key. 101 00:08:15 --> 00:08:20 In the next iteration of foreach, Name key is returned. 102 00:08:21 --> 00:08:25 Note: Hash is an unordered collection of data. 103 00:08:26 --> 00:08:32 So, keys returned will not be in the sequence defined at the time of creating hash. 104 00:08:33 --> 00:08:37 The loop on values works in a similar way. 105 00:08:38 --> 00:08:40 Press Ctrl + S to save the file. 106 00:08:41 --> 00:08:45 Then, switch to terminal and execute the Perl script as 107 00:08:46 --> 00:08:49 perl loopingOverHash dot pl 108 00:08:50 --> 00:08:52 and press Enter. 109 00:08:53 --> 00:08:57 The following output is displayed on the terminal. 110 00:08:58 --> 00:08:58 Let us summarize. 111 00:08:59 --> 00:09:00 In this tutorial, we learnt - 112 00:09:01 --> 00:09:02 Hash in Perl and 113 00:09:03 --> 00:09:04 Accessing elements of a hash 114 00:09:05 --> 00:09:07 using sample programs. 115 00:09:08 --> 00:09:10 Here is assignment for you - 116 00:09:11 --> 00:09:14 Declare hash having student name as key 117 00:09:15 --> 00:09:17 And his/her percentage as the value. 118 00:09:18 --> 00:09:23 Loop over hash using keys, values and each function 119 00:09:24 --> 00:09:28 Then print the percentage of each student. 120 00:09:29 --> 00:09:31 Watch the video available at the following link 121 00:09:32 --> 00:09:36 It summaries the Spoken Tutorial project 122 00:09:37 --> 00:09:41 If you do not have good bandwidth, you can download and watch it 123 00:09:42 --> 00:09:48 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials 124 00:09:49 --> 00:09:52 Gives certificates to those who pass an online test 125 00:09:53 --> 00:10:01 For more details, please write to contact at spoken hyphen tutorial dot org 126 00:10:02 --> 00:10:05 Spoken Tutorial Project is a part of the Talk to a Teacher project 127 00:10:06 --> 00:10:14 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 128 00:10:15 --> 00:10:25 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro 129 00:10:26 --> 00:10:29 Hope you enjoyed this Perl tutorial. 130 00:10:30 --> 00:10:32 This is Amol signing off. 131 00:10:33 --> 00:10:38 Thanks for joining.