in reply to (jeffa) Re: simple menu loop problems
in thread simple menu loop problems

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); }

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