Perl Hash Tables

Copy the text below and run it as a Perl script.


# ASSIGNING HASH VALUES
#
# hash tables consist of key/value pairs; they are kept
#  internallyas a list in which every key is followed by a value;
# therefore, values can be assigned to hash tables as an
# even-numbered list

%states = ("Sacramento", "California", "Madison", "Wisconsin", "Albany", "New York");

# another notation is more explicit about the relationship
# of key and value; it uses the => operator to identify the
# key to the left, and the value to the right; if the => operator
# encounters bare words in key positions, they will be automatically 
# quoted (note "New York", however, which consists of two words 
# and MUST be quoted)

%capitals = (California => "Sacramento", Wisconsin => "Madison" , "New York" => "Albany");

print "1. ACCESSING INDIVIDUAL HASH ELEMENTS\n\n";

# accessing an individual value of a hash is similar
# to accessing array elements: the variable name is prefix
# with the $ symbol, then the quoted keyword is add in
# curly braces {  }

print "Sacramento is the capital of: " . $states{"Sacramento"} . "\n\n";

# If the hash variable appears within a quoted string,
# the key must be in single quotes

print "The capital of New York is: $capitals{'New York'}\n\n";

# -------------------------------------------------------- #

print "2. PROCESSING EVERY ELEMENT OF A HASH TABLE\n\n";

# the keys() and values() functions take a hash as
# an argument and return an array of all keys or
# values, respectively

@myKeys = keys(%capitals);
print "\%capitals\' keys are:\n";

for ($i = 0; $i < @myKeys; ++$i)
{
   print "$myKeys[$i]" . "\n";
}
print "\n";


# values() returns the all values of the hash table
# with no association to keys

@myVals = values(%capitals);
print "\%capitals\' values are:\n";

for ($i = 0; $i < @myVals; ++$i)
{
   print "$myVals[$i]" . "\n";
}
print "\n";


# with the keys array, it is possible to iterate 
# of every key in the hash and retrieve its value

@myKeys = keys(%capitals);

# the foreach loop iterates over each element of
# of the @myKeys array; the control variable $element
# is the array element of the current iteration

print "using each key of \%capitals to retrieve values:\n\n";
foreach $element (@myKeys)
{
    print "$element\'s capital is " . $capitals{$element} . ".\n";
}

print "\n\n";

# -------------------------------------------------------- #

print "3. DELETING KEYS FROM A HASH\n\n";

# the delete() function takes a scalar value as
# an argument, deletes the key and returns the 
# deleted value, provided the key existed 
# in the hash

$deleted = delete($states{'Albany'});
print "deleted from \%states: $deleted\n\n";

@myKeys = keys(%states);
print "hash \%states now contains:  ";
foreach $element (@myKeys)
{
    print "$element / " . $states{$element} . "   ";
}

print "\n\n";

# -------------------------------------------------------- #

print "4. TESTING IF A KEY EXISTS\n\n";

# the exists() function takes hash key,
# $hash{$key}, as an argument and tests 
# whether the hash has a corresponding
# key; it returns a 'true' of 'false'
# value that can be used in a conditional
# statement

print "looking for \'$deleted\'\n\n";

if (exists($states{$deleted}))
{
   print "$deleted exists\n\n";
}
else
{
   print "$deleted has wiped from the face of the earth!\n\n";
}

# -------------------------------------------------------- #

print "5. ADDING A NEW VALUE\n\n";

# adding new key/value pairs to the
# hash is straight forward: simply
# assing a value to a key

$states{"Providence"} = 'Rhode Island';

print "hash \%states now contains:    ";

foreach $element (keys(%states))
{
    print "$element / " . $states{$element} . "   ";
}

print "\n\n";