#!/share/bin/perl -w # Two hashes - one maps roles to players, the other maps # players to roles. # It is assumed that each player takes only one role. # There are lots of ways to initialize a hash # Element by element #$cast{"First witch"} = "Angie"; #$cast{"Second witch"} = "Karen"; # Or create a list literal and convert to hash (list assumed to # be ( key value key value ....) @cast = ("First witch", "Angie","Second witch", "Karen","Third witch", "Sonia", "Duncan","Peter","Macbeth", "Phillip", "Lady Macbeth", "Joan", "Banquo", "John","Porter", "Neil", "Gentlewoman 3", "Holly"); # and if that gets a little opaque, there is another way # %cast = ("First witch" => "Angie", # "Donaldbain" => "Willy", # "Menteith" => "Tim", # "Gentlewoman 3" => "Holly"); %cast = @cast; %players = reverse %cast; while(1) { print 'Enter command: 1) Look up who plays role 2) Look up role played by actor 3) List roles 4) Quit Command: '; $cmd = ; if($cmd == 4) { last; } elsif($cmd == 3) { # keys gives a list of the keys in the hash @roles = keys %cast; # "foreach" loop through list for $role (@roles) { print $role, "\n"; } } elsif($cmd == 2) { print "Enter actor's name : "; $Player = ; chomp($Player); # simple hash lookup operation $Role = $players{$Player}; if($Role) { print "$Player plays the role $Role\n"; } else { print "$Player is resting for this production\n"; } } elsif($cmd == 1) { print "Enter role name : "; $Role = ; chomp($Role); $Actor = $cast{$Role}; if($Actor) { print "$Role is being performed by $Actor\n"; } else { print "$Role not recognized as part of cast\n"; } } else { print "Command not recognized\n"; } }