on Wed, 05 Feb 2003 06:52:18 GMT, [EMAIL PROTECTED] (Gary
Merrick) wrote:
> I need to somehow test for user input being
> numeric. I think. <:-)
This is a frequently asked question:
perldoc -q number/whole
> my $input = 'undef';
Here you are putting the literal string 'undef' into $input. Did you
mean to write
my $input = undef;
> until ($input eq "exit") {
> print "\nPlease type one of [list, imdb_number, exit]: ";
> chomp ($input = <STDIN>);
> if ("$input" eq "list") {
You don't need double quotes around $input.
> # do stuff
> }
> elsif ("$input" eq "exit") {
> # Do nothing.
You don't want to do nothing here (you want to exit;-)
> # Without this part, user gets "Invalid input" message before
> exiting. }
> # This part seems to catch any kind of invalid input, and it
> # never gets
> # to "else". Why? I guess I need to test for
> # numeric input (if it's not, # it goes to "else"), and then
> # see if the number matches one of these:
> elsif ("$input" ==
> "0120611"||"0106308"||"0088247"||"0267804"||"0094074"||
> "0102798"||"0120382"||"0196229"||"0272152"||"0109830") {
First, since you are comparing strings (note the leading zeroes in
your "numbers"), you need 'eq' instead of '=='.
Second, 'eq' (and '==') have higher precedence than '||', so
$a eq "a" || "b"
really means
($a eq "a") || "b"
This expression is always true, even if $a is not equal to "a", since
"b" is true. You need to write
$a eq "a" or $a eq "b"
If you have a lot of values to compare against, you could use a hash:
my %wanted = ();
$wanted{$_} = 1 for qw(a b);
if ($wanted{$input}) {
# do stuff
}
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]