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

can someone tell me why this doesn't loop through over and over until i enter number 7 to exit. I'm pretty much a rookie but trying, also if there is a better or cleaner way that is not to confusing I would be interested in your opinions. Thanks Bob
#!/usr/bin/perl -w START: { &menu; $input= (<STDIN>); chomp $input; if ($input == 1) { print "You have chosen option 1\n"; } if($input == 2) { print "You have chosen option 2\n"; } if ($input == 3) { print "You have chosen option 3\n"; } if($input == 4) { print "You have chosen option 4\n"; } if ($input == 5) { print "You have chosen option 5\n"; } if($input == 6) { print "You have chosen option 6\n"; } else { exit; } #redo START unless $input >= 7; redo START; } sub menu { print "Enter a number for the option you want\n"; print "1) Cancel FINAL Schedule for machine A: \n"; print "2) Submit new FINAL schedule: \n"; print "3) Ignore Wrkld: \n"; print "4) Run around the block: \n"; print "5) Buy a new car: \n"; print "6) order Pizza: \n\n"; print "7) exit: \n\n"; }

Replies are listed 'Best First'.
Re: simple menu loop problems
by dash2 (Hermit) on Mar 12, 2002 at 16:40 UTC
    How about

    while (($input = <STDIN>) < 7)

    ?

    And the reason it is exiting is that you are using if, not elsif. So this bit:

    if($input == 6) { print "You have chosen option 6\n"; } else { exit; }

    always exits unless you choose option 6. You could have found out how your program was working with the debugger: perl -d program.pl, then use 's' or 'n' to step through each line of your program.

    dave hj~

(jeffa) Re: simple menu loop problems
by jeffa (Bishop) on Mar 12, 2002 at 17:14 UTC
    Lots of correct answers so far ... but, come on folks! Redundant hard-coded data?!?! Bleh. :|
    use strict; my $input; my @choice = ( 'Cancel FINAL Schedule for machine A', 'Submit new FINAL schedule', 'Ignore Wrkld', 'Run around the block', 'Buy a new car', 'order Pizza', 'exit', ); while (1) { menu(); chomp ($input = <>); last if $input == 7; print "You have chosen option $input\n"; } sub menu { print "Enter a number for the option you want\n"; print map { ($_+1).": $choice[$_]\n" } (0..$#choice); }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      okay i understand the array part, but what is
      - while (1) doing (is that saying "while True"?)
      - menu() what is this doing?
      - what is this line doing:
      print map { ($_+1).": $choice$_\n" } (0..$#choice);
      Thanks
      Bob
      my $input; my @choice = ( 'Cancel FINAL Schedule for machine A', 'Submit new FINAL schedule', 'Ignore Wrkld', 'Run around the block', 'Buy a new car', 'order Pizza', 'exit', ); while (1) { menu(); chomp ($input = <>); last if $input == 7; print "You have chosen option $input\n"; } sub menu { print "Enter a number for the option you want\n"; print map { ($_+1).": $choice[$_]\n" } (0..$#choice); }
        Hey ddrumguy! (got your email BTW ;))

        1. Yes. while(1) is saying while TRUE ... the reason i use that is because Perl has the last token to exit. I used to be afraid of while(1) loops, but i like them now.
        2. menu() is just calling the menu subroutine. tye gets on to me for calling subs this way ... a more proper way would be to use the ampersand: &menu(). The main reason i can think of is ... what if a new built-in function named menu is introduced? Then menu() would call it, not the one you declared - but &menu() will call yours.
        3. Last item ... hehehe sorry about that. How about this instead:
        my $total_choices = $#choice; for my $index (0..$total_choices) { print $index + 1, ": $choice[$index]\n"; }
        Same thing ;)

        Keep it up, you'll be coding map's and grep's and Schwartzian Transforms in no time. ;)

        Still get good ddrum offers? ;)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: simple menu loop problems
by gellyfish (Monsignor) on Mar 12, 2002 at 16:41 UTC

    There is no loop block here for redo to operate on - I am surprised it doesn't give an error actually. You should place make your outer block a loop like:

    #!/usr/bin/perl -w use strict; my $input = 0; START: while($input != 7 ) { menu(); chomp($input = <STDIN>); if ($input == 1) { print "You have chosen option 1\n"; next START; } if($input == 2) { print "You have chosen option 2\n"; next START; } if ($input == 3) { print "You have chosen option 3\n"; next START; } if($input == 4) { print "You have chosen option 4\n"; next START; } if ($input == 5) { print "You have chosen option 5\n"; next START; } if($input == 6) { print "You have chosen option 6\n"; next START; } else { last START; } } # omitted sub ...

    Also bear in mind that nice indentation is next to godliness :)

    /J\

Re: simple menu loop problems
by ropey (Hermit) on Mar 12, 2002 at 16:43 UTC
    This is one way of achieving your goal, i'm assuming the end outcome won't be to print what option you have selected.
    #!/usr/bin/perl -w use strict; check_input(); sub check_input { CHECK : while (1) { &menu; my $input= (<STDIN>); chomp $input; print "You have chosen option 1\n" if ($input == 1); print "You have chosen option 2\n" if ($input == 2); print "You have chosen option 3\n" if ($input == 3); print "You have chosen option 4\n" if ($input == 4); print "You have chosen option 5\n" if ($input == 5); print "You have chosen option 6\n" if ($input == 6); last CHECK if ($input == 7); } print "Finished\n"; } sub menu { print "Enter a number for the option you want\n"; print "1) Cancel FINAL Schedule for machine A: \n"; print "2) Submit new FINAL schedule: \n"; print "3) Ignore Wrkld: \n"; print "4) Run around the block: \n"; print "5) Buy a new car: \n"; print "6) order Pizza: \n\n"; print "7) exit: \n\n"; }
    Hope that helps
      save yourself some typing, ropey!

      print "You have chosen option $input\n" if ($input >= 1 && $input <= 6 +);

      ~Particle ;Þ

        I would agree however .... what happens if the code changes to call different functions instead of printing ??? I was presuming the code supplied was an example ! :)
Re: simple menu loop problems
by defyance (Curate) on Mar 12, 2002 at 19:35 UTC
    Probably not as cool as all the others, but it is simmilar. Just an idea for you. I made a couple of menu scripts for work stuff.. All I know is it works just the same..

    #!/usr/bin/perl -w use strict; my $read; sub menu { print "1) Cancel FINAL Schedule for machine A\n"; print "2) Submit new FINAL schedule\n"; print "3) Ignore Wrkld\n"; print "4) Run around the block\n"; print "5) Buy a new car\n"; print "6) order Pizza\n"; print "7) exit\n"; } for (;;) { &menu; chomp($read = <STDIN>); exit 0 if $read=~ /7/; print "You have chosen option $read\n"; print "Enter a number for the option you want\n"; }


    -- Yes, I am a criminal. My crime is that of defyance.