Determine whether a hash value exists, is defined, or is true :
1.print "Value EXISTS, but may be undefined.\n" if exists $hash{ $key };
2.print "Value is DEFINED, but may be false.\n" if defined $hash{ $key };
3.print "Value is TRUE at hash key $key.\n" if $hash{ $key };
Showing posts with label Perl Datastructures. Show all posts
Showing posts with label Perl Datastructures. Show all posts
Wednesday, 2 December 2009
Hash of Hashes
Create a hash of hashes via references:
The following two solutions are equivalent, except for the way the look.
Solution 1 :
$requiredPatches_href->{ $patch }->{ os } = $os;
$requiredPatches_href->{ $patch }->{ arch } = $arch;
$requiredPatches_href->{ $patch }->{ info } = $info;
Solution 2 : ( It is much clearer)
$requiredPatches_href->{ $patch } = {
os => $os,
arch => $arch,
info => $info,
};
Here is a small program to extract users their login name, uid,homedir and gid from /etc/passwd using HashOfHashes and hash reference:
login: bin uid:2 dir:/bin gid:2
login: clamav uid:110 dir:/var/lib/clamav gid:120
login: nobody uid:65534 dir:/nonexistent gid:65534
login: lp uid:7 dir:/var/spool/lpd gid:7
The following two solutions are equivalent, except for the way the look.
Solution 1 :
$requiredPatches_href->{ $patch }->{ os } = $os;
$requiredPatches_href->{ $patch }->{ arch } = $arch;
$requiredPatches_href->{ $patch }->{ info } = $info;
Solution 2 : ( It is much clearer)
$requiredPatches_href->{ $patch } = {
os => $os,
arch => $arch,
info => $info,
};
Here is a small program to extract users their login name, uid,homedir and gid from /etc/passwd using HashOfHashes and hash reference:
Output looks like this:
#!/usr/bin/perl -w
sub func
{
my ( $login, $p, $uid, $gid, $gecos, $dir, $s );
my %HoH = ();
my $file = '/etc/passwd';
open( PASSWD, "lt; $file" ) or die "Can't open $file : $!";
while( <passwd> )
{
( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );
#writing a hash of hash using reference
$HoH{$login} = {
'uid' => $uid,
'gid' => $gid,
'dir' => $dir,
};
}
close PASSWD;
return \%HoH;
}
my $hash_ref = &func;
# Print the keys and values of a hash, given a hash reference
while( my ($k, $v) = each %$hash_ref )
{
# Print Outer hash HOH keys and values
print "login: $k ";
# Print Inner hash keys and values for each outer hash key i.e login name
while ( my ($key,$val) = each %$v )
{
print "$key:$val ";
}
print "\n";
}
login: bin uid:2 dir:/bin gid:2
login: clamav uid:110 dir:/var/lib/clamav gid:120
login: nobody uid:65534 dir:/nonexistent gid:65534
login: lp uid:7 dir:/var/spool/lpd gid:7
Monday, 9 November 2009
Perl Hash
A hash is a simple collection of keys and their corresponding values. The values can be homogeneous or heterogeneous data types.
Example :
Add a key/value pair to a hash :
In the solutions below, quotes around the keys can be omitted when the keys are identifiers.
Hash:
Hash reference:
You can also store references to functions in your data structures, just as you can store references to arrays or hashes:
In the second to last line, we check whether the specified command name (in lowercase) exists in our "dispatch table", %HoF. If so, we invoke the appropriate command by dereferencing the hash value as a function and pass that function an empty argument list. We could also have dereferenced it as &{ $HoF{lc $cmd} }(), or simply $HoF{lc $cmd}().
Example :
%hash = (
key1 => 'value1',
key2 => 'value2',
key3 => 'value3',
);
Add a key/value pair to a hash :
In the solutions 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
You can also store references to functions in your data structures, just as you can store references to arrays or hashes:
%HoF = ( # Compose a hash of functions
exit => sub { exit },
help => \&show_help,
watch => sub { $watch = 1 },
mail => sub { mail_msg($msg) },
edit => sub { $edited++; editmsg($msg); },
delete => \&confirm_kill,
);
if ($HoF{lc $cmd}) { $HoF{lc $cmd}->() } # Call function
else { warn "Unknown command: `$cmd'; Try `help' next time\n" }
In the second to last line, we check whether the specified command name (in lowercase) exists in our "dispatch table", %HoF. If so, we invoke the appropriate command by dereferencing the hash value as a function and pass that function an empty argument list. We could also have dereferenced it as &{ $HoF{lc $cmd} }(), or simply $HoF{lc $cmd}().
Subscribe to:
Posts (Atom)