Variable Type: Hashes

                                                                                                                         

 

Hashes

  • A hash is an unordered collection of data which may contain many sets of data, but there is no “first” or “last”, no “top” or “bottom” item in a hash.
  • A hash is a set of key/value pairs.
  • Hashes are treated much like lists, but the hash variable is used. Hash variables all start with %.

Creating a Hash:

%hash1=(“Intel”,“Pentium”, “AMD”, “Athlon”);

  • To make relationships in hashes clearer, Perl allows the use of the => operator.

%hash1=(“Intel”=>“Pentium”, “AMD”=> “Athlon”);

  • When you have a pair like this:

Intel => Pentium

  • The left side of the pair is called the hash key, and the right side is the value.
  • The hash key is used to look up the pair.
  • All hash keys must be unique.
  • Fetching specific entry :

To locate a specific entry in a hash, we use the hash keys. The key is enclosed in curly braces:

%hash1=(Intel => Pentium, AMD => Athlon)

print $hash1{AMD};

Result: Athlon

 

Extracting Keys and Values :

  • You can get a list of all of the keys from a hash by using keys function .

Syntax:

keys %HASH

  •  This function returns an array of all the keys of the named hash.

Example:

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);

@names = keys %data;

print @names.

Result: John Paul,Lisa,Kumar

 

  • Similarly you can use values function to get a list of all the values.

Syntax:

      values %HASH

       This function returns a normal array consisting of all the values of the named hash.

Example:

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);

@names = values %data;

print @names.

Result: 45 30 40

 

Adding entry in hash:

  • You can add entries to a hash by specifying the key and value:

%hash1=(Intel => Pentium,  AMD => Athlon);

$hash{HP} = “PARISC”;

Deleting hash entries:

  • To remove an entry from a hash, the delete function is used with the hash key:

%hash1=(Intel => Pentium, AMD => Athlon);

delete ${AMD};

 

Banking a hash:

  • To blank this hash, just redefine it:

%hash=();

Getting Hash Size :

  • You can get the size – that is, the number of elements from a hash by using scalar context on either keys or values. Simply saying first you have to get an array of either the keys or values and then you can get size of array as follows:

Example:

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);

@keys = keys %data;

$size = @keys;

Result: 3

 

Checking for Existence :

  • If you try to access a key/value pair from a hash that doesn’t exist, you’ll normally get the undefined value, and if you have warnings switched on, then you’ll get a warning generated at run time.
  • You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be:

Example

#!/usr/bin/perl

%data = (‘John Paul’ => 45, ‘Lisa’ => 30, ‘Kumar’ => 40);

if( exists($data{‘Lisa’} ) ) {

    print “Lisa is $data{‘Lisa’} years old\n”;

}

else {

     print “I don’t know age of Lisa\n”;

}

Result : Lisa is 30 years old.

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *