Hashes In Perl

Walden Systems Geeks Corner Hashes In Perl tutorial programming how to Rutherford NJ New Jersey NYC New York North Bergen County
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

A Perl hash is defined by key-value pairs. Perl stores elements of a hash in a way that we can look up its values based on keys very fast. Like a scalar or In arrays, we use indices to access its elements/a>. In hash, we must use descriptive keys to access hash's element. A hash is sometimes referred to as an associative array. Like a scalar or an array variable, a hash variable has its own prefix. A hash variable must begin with a percent sign ( % ). The prefix % looks like key / value pair so remember this trick to name the hash variables.

A hash is an un-ordered group of key-value pairs. The keys are unique strings. The values are scalar values. Each value can be either a number, a string, or a reference. There are two major differences between arrays and hashes in Perl. Arrays are ordered, and we access an element of an array using its numerical index. Hashes are un-ordered and you access a value using a key which is a string. Each hash key is associated with a single value and the keys are all unique inside a single hash structure. That means no repetitive keys are allowed.

1   %age ;
2   $age{'John'} = 18 ;


Line 1 creates an empty has and line 2 inserts a key value pair into the hash. in this case, 'John' is the key and 18 is the value.


Creating hashes

Hashes are created in one of the two ways. The first way, we assign a value to a named key one at a time. In the second case, we use a list, which is converted by taking individual pairs from the list. the first element of the pair is used as the key, and the second, as the value. To make the code easier to read, Perl provides the => operator as an alternative to a comma. It helps differentiate between keys and values, and makes the code easier to read. When we see the => operator, you know that you are dealing with a hash.

Adding one at a time

$age{'John'} = 18;
$age{'Jane'} = 20;
$age{'Jack'} = 24;



Adding hashes as a list
%age = ('John', 18, 'Jane', 20, 'Jack', 24);



Using the =>
%age = ('John' => 18, 'Jane' => 20, 'Jack' => 424);

Accessing elements

To access elements from a hash, we must prefix the variable with a $ and then append the element key within curly brackets after the name of the variable. We can extract slices of a hash just as we can extract slices from an array. We will need to use the @ prefix for the variable to store the returned value because they will be a list of values. We can get a list of all of the keys from a hash by using keys function. Similarly, you can use values function to get a list of all the values.

1    %age = ('John' => 18, 'Jane' => 20, 'Jack' => 424);
2
3    print "$age{'John'}
";
4    @array = @age{'John', 'Jane'};
5    @names = keys %age;
6    @ages = values %age;


Line 3 access the value for the key 'John.' Line 4 will result in an array with 18 and 20. Line 5 will result in an array with 'John','Jane', 'Jack.' Line 6 will result in an array with 8, 20, 24.