Broad Network


Perl One Dimensional Hash Functions on Hash of Arrays

Perl Two-Dimensional Structures – Part 2

Foreword: In this part of the series, I talk about the one-dimensional hash functions on a Perl hash-of-arrays data structure.

By: Chrysanthus Date Published: 2 Apr 2016

Introduction

This is part 2 of my series, Perl Two-Dimensional Structures. In this part of the series, I talk about the one-dimensional hash functions on a Perl hash-of-arrays data structure. To create a data structure of a hash of arrays you start with a hash that runs down in one dimension. Each row of the structure is an array that fits into an element of the hash. A row is actually the value of a key/value pair. The real value of the key/value pair is a reference to a row. And so you can use the hash one-dimensional functions on the hash-of-arrays structure. Instead of acting on a value of the key/value pair, a one-dimensional hash function will act on a row that is the effective value of the key/value pair. You should have read the previous parts of the series, as this is a continuation.

The each Function on a Hash-of-Arrays
The syntax for the each function of a one-dimensional hash is,

    my ($key, $value) = each (%HashName)

For the one-dimensional hash, the each function returns the next key/value pair of the hash. The each function is used for a hash-of arrays in the same way, but the value is a reference to an array. The key is still whatever string you typed in. To dereference the reference variable, just precede it with a $ symbol and use the square brackets indexing to obtain an array value, as in,

    $$value[i]

When you dereferemce an array reference, you obtain an array as if it was, @arr. Read and try the following code that reads the second values of two rows of a hash-of-arrays structure:

use strict;

    my %Ha;

    $Ha{'Header'} = ["Geography", "History", "Physics", "Math"];
    $Ha{'Joan Mary'} = [50, 64, 14, 85];
    $Ha{'John Smith'} = [78, 20, 85, 55];
    $Ha{'Suzan Wright'} = [22, 33, 44, 55];
    $Ha{'Peter Jones'} = [66, 77, 88, 99];
    $Ha{'James Bond'} = [52, 42, 32, 22];
    $Ha{'John Rambo'} = [71, 72, 73, 74];
    $Ha{'Spider Man'} = [75, 76, 77, 78];
    $Ha{'Iron Man'} = [81, 80, 79, 78];


    my ($key0, $value0) = each(%Ha);
    my $var0 = $$value0[1];
    my ($key1, $value1) = each(%Ha);
    my $var1 = $$value1[1];

    print $var0, "\n";
    print $var1, "\n";

Note: the 0 and 1 in the variables $value0 and $value1 are part of the names and nothing else. Remember, the order in which you type the key/value (keys/row) pairs, is not necessarily the order in which the elements (key/value pairs) are read or printed.

The keys Function
The syntax of the keys function for a one-dimensional hash is,

    keys (%hashName)

It returns an array of all the keys in the hash. For a two-dimensional hash-of-arrays structure, it still returns all the keys of the hash-of-arrays. The order of the return keys is not necessarily the order in which the keys were typed. Remember, a hash-of-arrays truly has but key/array-reference pairs. Read and try the following code, which displays two keys of the hash-of-arrays:

use strict;

    my %Ha;

    $Ha{'Header'} = ["Geography", "History", "Physics", "Math"];
    $Ha{'Joan Mary'} = [50, 64, 14, 85];
    $Ha{'John Smith'} = [78, 20, 85, 55];
    $Ha{'Suzan Wright'} = [22, 33, 44, 55];
    $Ha{'Peter Jones'} = [66, 77, 88, 99];
    $Ha{'James Bond'} = [52, 42, 32, 22];
    $Ha{'John Rambo'} = [71, 72, 73, 74];
    $Ha{'Spider Man'} = [75, 76, 77, 78];
    $Ha{'Iron Man'} = [81, 80, 79, 78];

    my @keys = keys(%Ha);

    print $keys[0], "\n";
    print $keys[1], "\n";

In scalar context the function returns the number of keys in the hash-of-arrays.

The values Function
The syntax of the keys function for a one-dimensional hash is:

    values (%hashName)

It returns an array of all the values in the hash. For a two-dimensional hash-of-arrays structure, it returns all the array references of the arrays in the hash. The return list of references can be held in an array. The order of the return references is not necessarily the order in which the arrays were typed. Remember, a hash-of-arrays has key/array-reference pairs. Read and try the following code, which displays the first values of two of the arrays in a hash-of-arrays structure:

use strict;

    my %Ha;

    $Ha{'Header'} = ["Geography", "History", "Physics", "Math"];
    $Ha{'Joan Mary'} = [50, 64, 14, 85];
    $Ha{'John Smith'} = [78, 20, 85, 55];
    $Ha{'Suzan Wright'} = [22, 33, 44, 55];
    $Ha{'Peter Jones'} = [66, 77, 88, 99];
    $Ha{'James Bond'} = [52, 42, 32, 22];
    $Ha{'John Rambo'} = [71, 72, 73, 74];
    $Ha{'Spider Man'} = [75, 76, 77, 78];
    $Ha{'Iron Man'} = [81, 80, 79, 78];

    my @values = values(%Ha);

    print $values[0][0], "\n"; #referring to @values and not values(%Ha)
    print $values[1][0], "\n"; #referring to @values and not values(%Ha)

@values in the code has references to other arrays, and so it is a normal two-dimensional array, so you access a cell value using the two square bracket pairs. If you have $values instead of @values, meaning you have a reference to a two-dimensional array, you would have to double the $ symbol as well, to access the value of a cell.

The exists Function
The syntax for the exist function for a one-dimensional hash is,

    exists ($hash{'$key'})

It returns true if the key of the hash has ever been initialized (has ever had a value); if the key/value pair element does not exists, it returns false. For a two-dimensional hash-of-arrays structure it returns true if the key has a corresponding array, otherwise it returns false. Read and try the following code that illustrates this:

use strict;

    my %Ha;

    $Ha{'Header'} = ["Geography", "History", "Physics", "Math"];
    $Ha{'Joan Mary'} = [50, 64, 14, 85];
    $Ha{'John Smith'} = [78, 20, 85, 55];
    $Ha{'Suzan Wright'} = [22, 33, 44, 55];
    $Ha{'Peter Jones'} = [66, 77, 88, 99];
    $Ha{'James Bond'} = [52, 42, 32, 22];
    $Ha{'John Rambo'} = [71, 72, 73, 74];
    $Ha{'Spider Man'} = [75, 76, 77, 78];
    $Ha{'Iron Man'} = [81, 80, 79, 78];

    if (exists($Ha{'John Smith'}))
        {
            print "'John Smith' had been initialized with an array.", "\n";
        }
    if (!exists($Ha{'Mary Magnet'}))
        {
            print "There is no element with key, 'Mary Magnet'.", "\n";
        }

Note the use of the ! operator before one of the exists() function.

The delete Function
The syntax for the delete function for a one-dimensional hash is:

    delete ($hash{'$key'})

This function deletes an element in the hash, such that the exists() function does not return true for the element. That is, the key and corresponding value pair are deleted. For a two-dimensional hash-of-arrays structure, the meaning is the same but you have key/array pairs. Read and try the following code:

use strict;

    my %Ha;

    $Ha{'Header'} = ["Geography", "History", "Physics", "Math"];
    $Ha{'Joan Mary'} = [50, 64, 14, 85];
    $Ha{'John Smith'} = [78, 20, 85, 55];
    $Ha{'Suzan Wright'} = [22, 33, 44, 55];
    $Ha{'Peter Jones'} = [66, 77, 88, 99];
    $Ha{'James Bond'} = [52, 42, 32, 22];
    $Ha{'John Rambo'} = [71, 72, 73, 74];
    $Ha{'Spider Man'} = [75, 76, 77, 78];
    $Ha{'Iron Man'} = [81, 80, 79, 78];

    delete ($Ha{'John Smith'});
    if (!exists($Ha{'John Smith'}))
        {
            print "key 'John Smith' has never been initialized or never existed.", "\n";
        }

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message