in reply to help with user selected hash operations?
I have added the "o" option, so you can check results at each step.
Significant changes:#! /usr/bin/perl use strict; my %son_father; my $choice; my $name1; my $add_dad; %son_father = (Jeff => "Doug", Thomas => "Evan", Robert => "Jason", Bruce => "Richard", Clark => "Jon") ; my $menu = <<MENU; SON_FATHER Hash Operations a- Add a son-father pair d- Delete a son-father pair e- exit the program g- Get a father o- Output the hash neatly r- Replace a father x- get a grand father MENU while (1) { # display the menu print $menu, "\n\n"; # get the user choice print "Make your choice: "; chomp($choice = lc <STDIN>); # fulfill the user request if ($choice eq 'a'){ print "Enter a male name: "; chomp ($name1 = <STDIN>); if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; next; } print "Add a father: "; chomp ($add_dad = <STDIN>); $son_father{$name1} = $add_dad; print "Added\n"; next; # <<<<< added } if ($choice eq 'd') { print "Enter a male name: "; chomp ($name1 = <STDIN>); if (exists $son_father{$name1}) { delete $son_father{$name1}; print "Deleted.\n"; } else { print "Sorry, couldn't find '$name1' -- try again later!" +; } next; } if ($choice eq 'o') { print "$_\t=> $son_father{$_}\n" for sort keys %son_father; next; } if ($choice eq 'e') { print "Come back again -- goodbye!"; exit; }else { print "Invalid choice, TRY AGAIN!\n"; } }
All power corrupts, but we need electricity.
|
|---|