Sunday 1 March 2009

Learning Perl Hashes


1)Initialize (clear, or empty) a hash :

Assigning an empty list is the fastest method.

Solution

my %hash = ();

2)Initialize (clear, or empty) a hash reference :

Solution

my $hash_ref = {}; # a reference to an empty hash, ref will return HASH

The great thing about this is that if before performing an actual assignment, you want to determine (using the ref operator) the type of thingy that a reference is pointing to, you can!... and you can expect it to be a HASH built-in type, because that is what the line above initializes it to be.

Note :

If you treat the variable just as any scalar variable; and use the my declaration alone, or assign a value, ref will return false.

my $hash_ref;
my $hash_ref = 0; # zero

3)Add a key/value pair to a hash :

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

Solution

$hash{ 'key' } = 'value'; # hash

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

4)Hash reference :

Solution

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

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

5)Add several key/value pairs to a hash :

Solution

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',
);

6)Copy a hash :

Solution

my %hash_copy = %hash; # copy a hash

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

7)Delete a single key/value pair :

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

Solution

Hash:

delete $hash{$key};

Hash reference:

delete $hash_ref->{$key};

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

The actions below print the key/value pairs.

Solution

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";
}

9)A hash reference would be only slightly different:

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

Solution

Use keys with a for loop :

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

Example

my $file = $ARGV[0] || "-";

my %from = ();

open FILE, " < $file" or die "Can't open $file : $!";

while( <FILE> ) {
if (/^From: (.*)/) { $from{$1}++ } # count recurrences of sender
}

close FILE;

for my $sender ( sort keys %from ) {
print "$sender: $from{$sender}\n";
}

10)Get the size of a hash :

Solution

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

Solution

my $i = 0;

$i += scalar keys %$hash_ref; # method 1: explicit scalar context
$i += keys %$hash_ref; # method 2: implicit scalar context

11)Use hash references :

Solution

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";

12)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.

Solution

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

Solution

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

13)Function to build a hash of hashes; return a reference :

Solution

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;
}

15)Print the keys and values of a hash, given a hash reference :

Solution

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

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

Solution

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 };

No comments:

Post a Comment

Tweets by @sriramperumalla