Snowman11 has asked for the wisdom of the Perl Monks concerning the following question:

Hey im just learning this perl it i think its pretty cool. Im sure im just missing something reallly simiple but can't put my finger on it. Been looking for answers for hours, So i thought i might get some help. Heres what i have so far on this code snipplet:
######################### #Create the Menu Fuction# ######################### sub menu{ system "cls"; print "You have entered these Values for each of these fields."; print "If you wish to Fix any of the information now. Select Number to + Change:\n"; print "\t1. First Name: $fname\n"; print "\t2. Last name: $lname\n"; print "\t3. The School you work at: $school\n"; print "\t4. Your phone: $phone #\n"; print "\t5. Position: $position\n"; print "\t6. Role: $role\n"; print "\t7. Unique Password: $pass\n"; print "\t8. Done\n"; print "Type a number to Choose to fix: "; #----------------------If user Picks 1-------------------------------- +---# if(<STDIN>== int 1) { system "cls"; $fname = ""; while ($fname eq "") { printf "\n\nPlease Enter your First Name: "; $fname = <STDIN>; chomp($fname); &menu; }#end of while loop }#end of if(); if(<STDIN>== int 2) { system "cls"; $lname = ""; while ($lname eq "") { printf "\n\nPlease Enter your Last Name: "; $lname = <STDIN>; chomp($lname); &menu; }#end of while loop }#end of if(); }#end of sub menu menu;
Trying to make it so when the user pushes the number for the menu it will bring you to change the value of the variable but i can only get the first option to work and the others will not work at all :/

Replies are listed 'Best First'.
Re: Menu Troubles..
by samtregar (Abbot) on Jun 15, 2005 at 21:24 UTC
    Every time you evaluate <STDIN> you read a line from standard input. That means the first if() looks at the first line then the second if() looks at the next one, etc. Probably not what you meant! Instead, assign <STDIN> to a variable and then you can test it against multiple conditions.

    -sam

      thank you
Re: Menu Troubles..
by ktross (Deacon) on Jun 15, 2005 at 21:32 UTC
    Save each line of input to a variable and compare the variable like:
    # get input $input = <STDIN>; #----------------------If user Picks 1-------------------# if($input == int 1) { system "cls"; $fname = ""; while ($fname eq "") { printf "\n\nPlease Enter your First Name: "; $fname = <STDIN>; chomp($fname); &menu; }#end of while loop }#end of if(); #----------------------If user Picks 2-------------------# if($input == int 2) { system "cls"; $lname = ""; while ($lname eq "") { printf "\n\nPlease Enter your Last Name: "; $lname = <STDIN>; chomp($lname); &menu; }#end of while loop }#end of if();
    Update: samtregar beat me to the punch
      Thats awesome and it worked great! Thanx Just a quick question i can now go through and change all the values but i can only do it once. Wondering if i would need a while loop so that i can press "1" and enter value which changes the value of fname in this case then brings me back to the menu then press "1" to repeat this process.
        I'm not sure I understand quite what you mean, but when I ran the appropriately edited snippet, the script returned to the main menu because of the &menu call at the end of each if statment. A loop shouldn't be required. Just make sure that the <STDIN> statement is within the menu subroutine. Note: if no valid input is received you return from your subroutine, and it doesn't loop back to the main menu as written.
Re: Menu Troubles..
by nedals (Deacon) on Jun 15, 2005 at 22:55 UTC

    Your code could be greatly simplified with the use of a couple of arrays and a hash.

    Try something like this. (UNTESTED)

    my %hash; my @hashkey = qw(fname lname school phone position role pass); my @query = ('First Name','Last Name','The School you work at', etc... +. ); my $number = 0; while ($number < 8) { system "cls"; print "You have entered these Values for each of these fields."; print "If you wish to Fix any of the information now. Select Number +to Change:\n"; print "\t1. $query[0]: $hash{$hashkey[0]\n"; print "\t2. $query[1]: $hash{$hashkey[1]\n"; .. .. print "\t8. Done\n"; print "Type a number to Choose to fix: "; $number = <STDIN>; if ($number < 8 ) { system "cls"; while ($hash{$hashkey[$number]} eq "") { printf "\n\nPlease Enter your $query[$number]: "; $hash{$hashkey[$number]} = <STDIN>; chomp($hash{$hashkey[$number]}); } } } exit;

      I usually go a step further. I don't like having two related lists which aren't in the same data structure. So something like this:

      my @data = ( { query => 'First Name', }, { query => 'Last Name', }, # etc. ); while (1) { system("cls"); print "You have entered these values for each of these fields.\n", "If you wish to fix any of the information now, select the num +ber to change\n"; for (my $i = 0; $i < scalar @data; ++$i) { print "\t", $i + 1, ". ", $data[$i]{query}, ": ", $data[$i]{value} +||"", "\n"; } print "\t0. Done\n"; print "Type a number to choose to change: "; my $number = <STDIN>; chomp($number); if ($number == 0) { last; } --$number; print "\n\nPlease enter your ", $data[$number]{query}, ": "; $data[$number]{value} = <STDIN>; chomp($data[$number]{value}); }

      (Also untested.)

        Thanx Tanktalus This works fine what type of array is that?