in reply to Re^2: help with user selected hash operations?
in thread help with user selected hash operations?
You didn't quite get what kcott was telling you about your if statements and indentation. I've reformatted the code a little to hopefully help you see what he means:
$ cat pm_1202351.pl #! /usr/bin/perl use strict; my %son_father; my $choice; my $name1; my $name2; my $name3; my $name4; my $newname; 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 (my $name1 = ucfirst lc <STDIN>); } if (exists $son_father{$name1}) { #46 print "Duplicate name -- try again!\n"; } else { print "Add a father: "; chomp (my $add_dad = ucfirst lc <STDIN>); $son_father{$name1} = {$add_dad}; next; } if ($choice eq 'd') { print "Enter a male name: "; chomp (my $name2 = ucfirst lc <STDIN>); } if (exists $son_father{$name2}) { #59 delete $son_father{$name2}; } else { print "Sorry, couldn't find you -- try again later!"; next; } if ($choice eq 'e') { print "Come back again -- goodbye!"; last; next; } if ($choice eq 'g'){ print "Enter a son's name: "; chomp (my $name4 = ucfirst lc <STDIN>); } if (exists $son_father{$name4}) { #76 print "$son_father{$name4}\n"; next; } if ($choice eq 'o'){ my ($key, $value); foreach $value (sort keys %son_father){ print "$value\t$son_father{$value}\n"; next; } } if ($choice eq 'r'){ print "Enter a male name: "; chomp (my $name3 = ucfirst lc <STDIN>); if (exists $son_father{$name3}) { print "Enter a new father name: "; chomp ($newname = ucfirst lc <STDIN>); $son_father{$name3} = {$newname}; } else { print "Sorry, couldn't find you -- try again later!"; next; } } if ($choice ne 'a', $choice ne 'r', $choice ne 'o', $choice ne 'd', +$choice ne 'e', $choice ne 'g') { print "Invalid choice, TRY AGAIN!\n"; next; } }
Pay close attention to the lines marked 46, 59 and 76: You're intending them to be inside the prior if block, but it's hard to see that because your braces and indentation aren't consistent with each other.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: help with user selected hash operations?
by lunette (Acolyte) on Oct 30, 2017 at 18:52 UTC | |
by AnomalousMonk (Archbishop) on Oct 30, 2017 at 19:43 UTC | |
by roboticus (Chancellor) on Oct 30, 2017 at 19:48 UTC | |
by lunette (Acolyte) on Oct 30, 2017 at 20:27 UTC |