Welcome to the spoken tutorial on Hash in Perl. In this tutorial, we will learn about; Hash in Perl and Accessing element of a hash For this tutorial, I am using Ubuntu Linux12.04 operating system Perl 5.14.2 and gedit Text Editor You can use any text editor of your choice. To practise this tutorial, you should have knowledge of Variables & Data Structures in Perl. Knowledge of comments, loops, conditional statements and Arrays will be an added advantage. Please go through the relevant spoken tutorials on the spoken tutorial website. Hash is an unordered collection of data It’s a key/value pair data structure Hash keys are unique However, Hash can have duplicate values This is the declaration of a hash . Let us see how to get the value of a key from hash. The syntax for accessing the value of a key is dollar hashName open curly bracket single quote keyName single quote close curly bracket Let us understand hash using a sample program. I have already typed the code in perlHash dot pl file in gedit. Type the code as shown in your perlHash dot pl file. Hash in Perl is declared with percentage sign. These are the keys of hash. And these are the values of hash. Note: To access key of hash, one has to use dollar sign. Press Ctrl + S to save the file. Then switch to terminal and execute the Perl script as perl perlHash dot pl and press Enter. The output is as shown on the terminal. Now let us see add and delete of keys from hash. The syntax for . adding key is dollar hashName open curly bracket single quote KeyName single quote close curly bracket equal to $value semicolon 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. I have already typed the code in hashKeyOperations dot pl file. This is the declaration of hash. We’ll be adding, deleting the keys from this hash. Here we are adding a key to an already created 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. Switch to the terminal and execute the Perl script as perl hashKeyOperations dot pl and press Enter. Output will be as shown on the terminal. Let us look at sorting of a hash keys and values. Syntax to sort keys is sort open bracket keys percentage hashName close bracket semicolon Similarly, we can sort hash values as 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. Type the code as displayed on the screen in your sortHash dot pl file. Here we have declared hash of address. Here, to sort the keys we have used the sort inbuilt function, along with the keys function. This will sort the hash keys in alphabetical order. Similarly, we can use the sort function on values of hash. Sorting can also be done on numeric keys and/or values. Save the file and switch to terminal. Execute the script by typing perl sortHash dot pl and Press Enter The output will be as shown on the terminal. Now, let us see how to get all keys and values of hash. 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 Let us understand these using a sample program. For this, we’ll use perlHash dot pl script, which we have created earlier in this tutorial. Type the following piece of code as shown on the screen; Let us understand the code now. keys function on hash, returns an array which contains all keys of hash. values function on hash returns an array of values for all keys of hash. each function returns the key/value pair. Here, we have used the while loop. It will iterate over each key/value pair of hash, that is returned by each function. Press Ctrl+S to save the file. Now, let us execute the script on the terminal by typing perl perlHash dot pl And Press Enter The following output will be seen on the terminal. Now let us see few other ways of looping over hash. We can use foreach loop to iterate over each key of hash. Then perform a set of actions on the value of a key. The syntax is as displayed on the screen. Here, each iteration of foreach loop will assign key from hash to $variable. Then it will use that $variable to fetch the value or to perform a set of actions. Similarly, we can loop over hash values as shown on the screen. We will look at sample program. So, let me switch to loopingOverHash dot pl in gedit. Type the following piece of code as shown in your loopingOverHash dot pl This piece of code returns single key of hash. Here in our case, 1st time dollar key ($key) contains the Department as key. In the next iteration of foreach, Name key is returned. Note: Hash is an unordered collection of data. So, keys returned will not be in the sequence defined at the time of creating hash. The loop on values works in a similar way. Press Ctrl + S to save the file. Then, switch to terminal and execute the Perl script as perl loopingOverHash dot pl and press Enter. The following output is displayed on the terminal. Let us summarize. In this tutorial, we learnt – Hash in Perl and Accessing elements of a hash using sample programs. Here is assignment for you – Declare hash having student name as key And his/her percentage as the value. Loop over hash using keys, values and each function Then print the percentage of each student. Watch the video available at the following link It summaries the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org Spoken Tutorial Project is a part of the Talk to a Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro Hope you enjoyed this Perl tutorial. This is Amol signing off. Thanks for joining.