%[
#!/usr/interp/perl -w

# VARIOUS HASH EXAMPLES IN PERL
# By Louis Ziantz

# Description: This program is a demonstration of assigning
#              to hash values, building hashes from lists,
#              reversing hashes, and iterating through hashes.
# Procedure:   Simple value assignments are first shown.
#              Assignment of a list to a hash is shown.
#              The reverse of a hash being treated as a list
#              is carried out.  Two methods of iterating
#              through hashes are then illustrated.
# Input:       None.
# Output:      A hash is printed using two different methods
#              of iterating over it.

$a{345.2} = 456.5; # creates element 456.5 with key "345.2"
$a{"aaa"} = "bbb"; # creates element "bbb" with key "aaa"
$a{ccc} = "bBB"; # creates element "bbb" with key "aaa"

%building = (
	     "CS",   "Bahamas",
	     "CS",   "Amos Eaton",
	     "Math", "Amos Eaton",
	     "ECSE", "CII",
	     "Arch", "Greene"
	     );

%building = (
	     CS   => "Amos Eaton",   
	     Math => "Amos Eaton",
	     ECSE => "CII",
	     Arch => "Greene"
	    );

@building = %building;
# ("ECSE", "CII", "Math", "Amos Eaton", "Arch", "Greene", 
#  "CS", "Amos Eaton")
@dept = reverse(@building);
# ("Amos Eaton", "CS", "Greene", "Arch", "Amos Eaton", 
#  "Math", "CII", "ECSE")
%dept = @dept;
@dept = %dept;
# ("CII", "ECSE", "Greene", "Arch", "Amos Eaton", "Math")

foreach $key (keys (%building)) { # for each key of %building
    print("Key $key contains $building{$key}.\n");
}

while (($key,$value) = each(%building)) { # as above
    print("Key $key contains $value.\n");
}
%]


