in reply to Re: help with user selected hash operations?
in thread help with user selected hash operations?
oh, my gosh, this is more than i expected. thank you so much. T_T
i was hesitant to post the entire code because i thought it might be too long, but with little edits i've made so far, this is what i have:
#! /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}) { 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}) { 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}) { 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; } }
i can change my scalar variable names to make it easier to understand, these are just the names the professor asked us to use.
the problem i seem to be having is that the secondary STDINs seem to trigger the "invalid response", even with "next" as part of the code. along with that, if i do the "a" command first, the "duplicate name" prompt shows up in subsequent command loops... on top of that, i keep getting HASH(x) in place of the names i'm putting in for the secondary STDINs in the "a" and "r" commands. like this:
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 Make your choice: a Enter a male name: Bruce Duplicate name -- try again! Invalid choice, TRY AGAIN! 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 Make your choice: a Enter a male name: Phil Add a father: Justin 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 Make your choice: o Duplicate name -- try again! Bruce Richard Clark Jon Jeff Doug Phil HASH(0x6453d0) Robert Jason Thomas Evan Invalid choice, TRY AGAIN! 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 Make your choice: e Duplicate name -- try again! Come back again -- goodbye!
sorry, this is a lot. >_< i really do want to understand how to make this run perfectly, not just for my class, but just so i can use it again without getting this mixed up.
and thank you again for being so helpful. it makes me feel really welcome, despite how clueless i can be, haha.
EDIT: i forgot to mention - if i don't explicitly use "my (variable)" at the beginning of the code, i'm continuously told by my terminal AND my writing program that it isn't explicitly defined at all, regardless of if i used "my" within the chomp or not. i know i should be able to just use the "my" command within the STDIN perimeters, but no matter how i fiddle with it, it just tells me it's wrong!
|
|---|