Perl Hash

This is a quick reference of perl Hash operations. Hense, you can find all details in Perl Book.

Initialize a hash

Assigning an empty list is the fastest way.

    my %hash = ();

Initialize a hash reference

For a hash reference (aka hash_ref or href), assign a reference to an empty hash.

Solution

    my $hash_ref = {};  # ref will return HASH

This operation assigns a hash reference to a scalar variable, you can use the variable as a hash.

Clear (or empty) a hash

If you literally want to empty the contents of a hash:

    for (keys %hash)
    {
        delete $hash{$_};
    }

Clear (or empty) a hash reference

    for (keys %$href)
    {
        delete $href->{$_};
    } 

Add a key/value pair to a hash

In the example below, quotes around the keys can be omitted when the keys are identifiers.

Hash:

    $hash{ 'key' } = 'value';    # hash
$hash{ $key } = $value; # hash, using variables

Hash reference:

    $href->{ 'key' } = 'value';  # hash ref
$href->{ $key } = $value; # hash ref, using variables

Add several key/value pairs to a hash

The following statements are equivalent, though the second one is more readable:

    %hash = ( 'key1', 'value1', 'key2', 'value2', 'key3', 'value3' );
    %hash = (
        key1 => 'value1',
        key2 => 'value2',
        key3 => 'value3',
    ); 

Copy a hash

    my %hash_copy = %hash;  # copy a hash
    my $href_copy = $href;  # copy a hash ref 

Delete a single key/value pair

The solution differs for a hash and a hash reference, but both cases can use the delete function.

Hash:

    delete $hash{$key};

Hash reference:

    delete $hash_ref->{$key};

Perform an action on each key/value pair in a hash

The actions below print the key/value pairs.

Use each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.

    while ( my ($key, $value) = each(%hash) ) {
        print "$key => $value\n";
    }

A hash reference would be only slightly different:

    while ( my ($key, $value) = each(%$hash_ref) ) {
        print "$key => $value\n";
    }

Use keys with a for loop.

    for my $key ( keys %hash ) {
        my $value = $hash{$key};
        print "$key => $value\n";
    }

Get the size of a hash

    print "size of hash:  " . keys( %hash ) . ".\n";

Use hash references

    sub foo
    {
        my $hash_ref;
     
        $hash_ref->{ 'key1' } = 'value1';
        $hash_ref->{ 'key2' } = 'value2';
        $hash_ref->{ 'key3' } = 'value3';
     
        return $hash_ref;
    }
     
    my $hash_ref = foo();
     
    print "the keys... ", sort keys %$hash_ref, "...\n";

 

Create a hash of hashes; via references

The following two solutions are equivalent, except for the way the look. In my opinion the second approach is clearer.

    $requiredPatches_href->{ $patch }->{ os }    = $os;
    $requiredPatches_href->{ $patch }->{ arch }  = $arch;
    $requiredPatches_href->{ $patch }->{ info }  = $info;
    $requiredPatches_href->{ $patch } = {
                                          os    => $os,
                                          arch  => $arch,
                                          info  => $info,
                                        }; 

Function to build a hash of hashes; return a reference

    sub foo
    {
        my ( $login, $p, $uid, $gid, $gecos, $dir, $s );
     
        my %HoH = ();
     
        my $file = '/etc/passwd';
        open( PASSWD, "< $file" ) or die "Can't open $file : $!";
     
        while( <PASSWD> ) {
            ( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );
     
            $HoH{ $login }{ 'uid' } = $uid;
            $HoH{ $login }{ 'gid' } = $gid;
            $HoH{ $login }{ 'dir' } = $dir;
        }
     
        close PASSWD;
     
        return \%HoH;
    }

Access and print a reference to a hash of hashes

    my $rHoH = foo();
     
    my( $uid, $gid, $dir );
     
    for my $login ( keys %$rHoH ) {
     
        $uid =       $rHoH->{ $login }->{ 'uid' };   # method 1  most readable
        $gid =    ${ $rHoH->{ $login } }{ 'gid' };   # method 2
        $dir = ${ ${ $rHoH }{ $login } }{ 'dir' };   # method 3 least readable
     
        print "uid: $uid, gid: $gid, dir, $dir.\n";
    }
    my $rHoH = foo();
     
    for my $k1 ( sort keys %$rHoH ) {
        print "k1: $k1\n";
        for my $k2 ( keys %{$rHoH->{ $k1 }} ) {
            print "k2: $k2 $rHoH->{ $k1 }{ $k2 }\n";
        }
    }

Function to build a hash of hashes of hashes; return a reference

    sub foo
    {
        my %HoHoH = ();
     
        while( ... ) {
     
            if( /LOCATION:/ ) {
     
                ...
     
            } elsif( /MODULE:/ ) {
     
                $HoHoH{ $loc }{ $module_type }{ MODULE_NAME } = $module_name;
     
            } elsif( $ARGS_ALLOWED ) {
     
                $HoHoH{ $loc }{ $module_type }{ $arg_name } = $arg_value;
     
            }
     
        }
     
        return \%HoHoH;
    }

 

Access and print a reference to a hash of hashes of hashes

    my $rHoHoH = foo();
     
    for my $k1 ( sort keys %$rHoHoH ) {
        print "$k1\n";
     
        for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
            print "\t$k2\n";
     
            for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
                print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
            }
        }
    }

 

Print the keys and values of a hash, given a hash reference

    while( my ($k, $v) = each %$hash_ref ) {
        print "key: $k, value: $v.\n";
    }

Determine whether a hash value exists, is defined, or is true

    print "Value EXISTS, but may be undefined.\n" if exists  $hash{ $key };
    print "Value is DEFINED, but may be false.\n" if defined $hash{ $key };
    print "Value is TRUE at hash key $key.\n"     if         $hash{ $key };

Example

Let's say we execute an sql query where some of the resulting values may be NULL. Before attempting to use any of the values we should first check whether they are defined, as in the following code. Note that the subroutine sql_fetch_hashref() takes care of connecting to the database, preparing the statement, executing it, and returning the resulting row as a hash reference using DBI's fetchrow_hashref() method.

    my $answers = 'a,b,c,d,e';
     
    my $sql = "select max_time, $answers from questions " .
              'where question_number=?';
    my $hash_ref = sql_fetch_hashref( $sql, $q );
     
    my @answers = split ',', $answers;
     
    my $max_time = $hash_ref->{max_time} || '60';
     
    my $hash_ref_ans;
    for my $letter ( @answers ) {
        $hash_ref_ans->{ $letter } = $hash_ref->{ $letter }
            if defined $hash_ref->{ $letter };
    }

The for loop made a new hash of only defined key/value pairs.