in reply to Prompting via shell with default arguments
I feel inclined to replace the regular expression used in that grep tosub query { my ($str,@argv) = @_; my $answer; while (1) { print "$str ", $argv[0] ? "[$argv[0]]: " : ": "; chomp($answer = <STDIN>); last unless @argv; last if grep $answer =~ /^\Q$_\E$/i, @argv; } return $answer; }
... i see no need to use a regex if you have to match the entire string.last if grep $answer eq $_, @argv;
UPDATE:
Ahhh, i missed that you were allowing case insensitive
answers via the regex. Here is another version that uses
lc to match upper and lower cases. This code also
fullfills the requirement of allowing the user to pick a
default option. I missed that one the first time around ;)
I also noticed you changed your original code. It looks much better now, but you should replacesub query2 { my ($str,@argv) = @_; my $answer; while (1) { print "$str ", $argv[0] ? "[$argv[0]]: " : ": "; chomp($answer = <STDIN>); return $answer if !@argv or grep lc($answer) eq lc($_), @argv; return $argv[0] unless $answer; } }
$answer = $default if ($answer eq ""); # with this instead $answer ||= $default;
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: Prompting via shell with default arguments
by Azhrarn (Friar) on Feb 02, 2003 at 02:18 UTC | |
|
Re: (jeffa) Re: Prompting via shell with default arguments
by Azhrarn (Friar) on Feb 02, 2003 at 20:24 UTC |