RosettaCodeData/Task/Menu/Perl/menu.pl

19 lines
437 B
Perl
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
sub menu
{
my ($prompt,@array) = @_;
return '' unless @array;
print " $_: $array[$_]\n" for(0..$#array);
print $prompt;
$n = <>;
return $array[$n] if $n =~ /^\d+$/ and defined $array[$n];
2026-02-01 16:33:20 -08:00
return menu($prompt,@array);
2023-07-01 11:58:00 -04:00
}
@a = ('fee fie', 'huff and puff', 'mirror mirror', 'tick tock');
$prompt = 'Which is from the three pigs: ';
2026-02-01 16:33:20 -08:00
$a = menu($prompt,@a);
2023-07-01 11:58:00 -04:00
print "You chose: $a\n";