UPDATE: thank you everyone for your help!! it made a really big difference, and i feel like i understand what i'm doing, now. i've got the code working perfectly, and the assignment turned in. now i know not to be nervous about asking for help, heheh.

i am at the end of my rope here, honestly - i'm totally new to programming as a whole, and the textbook provided to us for my course reads like jibberish to me. i can only fully understand the logic behind a code if i work it out in a command terminal myself, and with this code... i've been trying all night to figure out how to get this while loop to work, and this is my last desperate attempt for help, after scouring forums and questions. here's the code:

#! /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 = lc <STDIN>); } if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; } else { print "Add a father: "; chomp ($add_dad = lc <STDIN>); $son_father{$name1} = {$add_dad}; } if ($choice eq 'd') { print "Enter a male name: "; chomp ($name1 = lc <STDIN>); } if (exists $son_father{$name1}) { delete $son_father{$name1}; } else { print "Sorry, couldn't find you -- try again later!"; } if ($choice eq 'e') { print "Come back again -- goodbye!"; exit; } else { print "Invalid choice, TRY AGAIN!\n"; } }

i'm near tears here.

it feels like nothing is working. no matter what i pick, "Add a father:" shows up. and it seems like "Invalid choice" shows up no matter what, as well, although that may just be because i haven't finished adding in every potential operation.

(then again, i'm not sure how to tell the script "if (input) does not exist, then (print)"? i haven't been able to find anything opposite of "exists" beyond using "else", but that has to be part of an "if" loop, doesn't it? should i just change it to an "if" loop? i'm wary of changing the code too much, as the skeleton was provided TO us, but at this point, i'd almost rather just start this code from scratch...)

i'm just not sure how to get the chomp prompt under choice "a" to actually work. i think i can figure out the other chomp prompts based on solving that one problem, but i'm just. stumped. the loop works, and choice "e" does break it, but i don't understand why "Add a father:" shows up no matter what i choose, or how to tell the script to perform an operation based on a choice not being valid, or to perform an operation based on a <STDIN> not existing within the hash...

i'm sorry this is such a long question (and one that perhaps doesn't have a very clear question at all), but i didn't know where else to go to get any help. my teacher isn't readily available, and, again, my textbook just makes me want to cry from frustration. i feel like an idiot for not being able to just understand the text, but i really don't get it. thank you in advance. T_T


In reply to 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.