English 中文(简体)
Perl - Hashes
  • 时间:2024-12-22

Perl - Hashes


Previous Page Next Page  

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..

Here is a simple example of using the hash variables −

#!/usr/bin/perl

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

print "$data{ John Paul } = $data{ John Paul }
";
print "$data{ Lisa } = $data{ Lisa }
";
print "$data{ Kumar } = $data{ Kumar }
";

This will produce the following result −

$data{ John Paul } = 45
$data{ Lisa } = 30
$data{ Kumar } = 40

Creating Hashes

Hashes are created in one of the two following ways. In the first method, you assign a value to a named key on a one-by-one basis −

$data{ John Paul } = 45;
$data{ Lisa } = 30;
$data{ Kumar } = 40;

In the second case, you use a pst, which is converted by taking inspanidual pairs from the pst: the first element of the pair is used as the key, and the second, as the value. For example −

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

For clarity, you can use => as an apas for , to indicate the key/value pairs as follows −

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

Here is one more variant of the above form, have a look at it, here all the keys have been preceded by hyphen (-) and no quotation is required around them −

%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);

But it is important to note that there is a single word, i.e., without spaces keys have been used in this form of hash formation and if you build-up your hash this way then keys will be accessed using hyphen only as shown below.

$val = %data{-JohnPaul}
$val = %data{-Lisa}

Accessing Hash Elements

When accessing inspanidual elements from a hash, you must prefix the variable with a dollar sign ($) and then append the element key within curly brackets after the name of the variable. For example −

#!/usr/bin/perl

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

print "$data{ John Paul }
";
print "$data{ Lisa }
";
print "$data{ Kumar }
";

This will produce the following result −

45
30
40

Extracting Spces

You can extract spces of a hash just as you can extract spces from an array. You will need to use @ prefix for the variable to store the returned value because they will be a pst of values −

#!/uer/bin/perl


%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);

@array = @data{-JohnPaul, -Lisa};

print "Array : @array
";

This will produce the following result −

Array : 45 30

Extracting Keys and Values

You can get a pst of all of the keys from a hash by using keys function, which has the following syntax −

keys %HASH

This function returns an array of all the keys of the named hash. Following is the example −

#!/usr/bin/perl 

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

@names = keys %data;

print "$names[0]
";
print "$names[1]
";
print "$names[2]
";

This will produce the following result −

Lisa
John Paul
Kumar

Similarly, you can use values function to get a pst of all the values. This function has the following syntax −

values %HASH

This function returns a normal array consisting of all the values of the named hash. Following is the example −

#!/usr/bin/perl 

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

@ages = values %data;

print "$ages[0]
";
print "$ages[1]
";
print "$ages[2]
";

This will produce the following result −

30
45
40

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 −

#!/usr/bin/perl

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

if( exists($data{ Lisa } ) ) {
   print "Lisa is $data{ Lisa } years old
";
} else {
   print "I don t know age of Lisa
";
}

Here we have introduced the IF...ELSE statement, which we will study in a separate chapter. For now you just assume that if( condition ) part will be executed only when the given condition is true otherwise else part will be executed. So when we execute the above program, it produces the following result because here the given condition exists($data{ Lisa } returns true −

Lisa is 30 years old

Getting Hash Size

You can get the size - that is, the number of elements from a hash by using the 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 the size of array as follows −

#!/usr/bin/perl

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

@keys = keys %data;
$size = @keys;
print "1 - Hash size:  is $size
";

@values = values %data;
$size = @values;
print "2 - Hash size:  is $size
";

This will produce the following result −

1 - Hash size: is 3
2 - Hash size: is 3

Add and Remove Elements in Hashes

Adding a new key/value pair can be done with one pne of code using simple assignment operator. But to remove an element from the hash you need to use delete function as shown below in the example −

#!/usr/bin/perl

%data = ( John Paul  => 45,  Lisa  => 30,  Kumar  => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size:  is $size
";

# adding an element to the hash;
$data{ Ap } = 55;
@keys = keys %data;
$size = @keys;
print "2 - Hash size:  is $size
";

# delete the same element from the hash;
delete $data{ Ap };
@keys = keys %data;
$size = @keys;
print "3 - Hash size:  is $size
";

This will produce the following result −

1 - Hash size: is 3
2 - Hash size: is 4
3 - Hash size: is 3
Advertisements