in reply to simple menu loop problems

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: simple menu loop problems
by ddrumguy (Acolyte) on Mar 12, 2002 at 19:50 UTC
    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)