Since you have attempted something .. here is corrected/working code for you to enhance/complete the missing options(r,x).

I have added the "o" option, so you can check results at each step.

#! /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"; } }
Significant changes:
* Added "next" statements to return to top of the loop after an option completes
* Changed "if" statements to enclose subordinate input (Asking for names)
* Corrected the "Add Father" assignment .. removing extra {}, which made it a hashref.
* Remove "lc" for Name inputs to allow correct case names.
* Added the "o" option.

                All power corrupts, but we need electricity.


In reply to Re: help with user selected hash operations? by NetWallah
in thread help with user selected hash operations? by lunette

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.