Perl Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Perl Hashes

Perl Hashes

shape Introduction

This chapter demonstrates about the Perl Hashes which is a set fo key/value pairs and these variables are preceded by the "%" sign and following are the concepts covered in this chapter.
  • About Perl Hashes

About Perl Hashes

shape Description

Hashes can hold many scalars as an array but the only different is which doesn't have any index instead of an index which have the keys and values. Hash can be declared by "%" and followed by the name of the hash and user can also use the hash name and followed by "$" and followed by the "key" which is associated with curly braces. The code below demonstrates the using of has variables as shown below. [perl] %data = ('Dany', 45, 'Antony', 30, 'Kate', 40); print "\$data{'Dany'} = $data{'Dany'}\n"; print "\$data{'Antony'} = $data{'Antony'}\n"; print "\$data{'Kate'} = $data{'Kate'}\n"; [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image. Which is very difficult to know individual ages user need to remember both index locations of age and name of the all people if a user has 3 names it is very easy to remeber but which have 1000 or more which not yet easy which is overcome by the hash. Hash can be created by assigning the values to a named key on a one-by-one basis is as shown below. [code] $data{'Dany'} = 45; $data{'Antony'} = 30; $data{'Kate'} = 40; [/code] In the another way hash can be created by using a list is converted by taking individual pairs in which the first element of the pair is used as the key and the second is value is as shown below. [code] %data = ('Dany', 45, 'Antony', 30, 'Kate', 40); [/code] Add Perl Hashes The user can also add new entries to the existed hash is as shown in below code. [perl] $newHash{'Dany'}=25; $newHash{'Antony'}=26; $newHash{'Kate'}=27; print %newHash; [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image. Perl delete Key The user may also delete the entries from a hash. The syntax below demonstrates to delete the entries as shown below. [code] delete $newHash{'Jim'};#This will delete an entry from the hash. [/code] Delete is an inbuilt function of hash The code below demonstrates the perl delete key as shown below. [perl] #!/usr/bin/perl %data = ('Dany' => 45, 'Antony' => 30, 'Kate' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size\n"; # adding an element to the hash; $data{'Ali'} = 55; @keys = keys %data; $size = @keys; print "2 - Hash size: is $size\n"; # delete the same element from the hash; delete $data{'Ali'}; @keys = keys %data; $size = @keys; print "3 - Hash size: is $size\n"; [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Summary

shape Key Points

  • Hashes hold any scalars and arrays.
  • Instead of index hash have keys and values.