Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allowable input param question 1

Status
Not open for further replies.

vego

Programmer
Jun 26, 2004
47
US
Hi, I'm just putting the final touches on a little rating-type script and in an attempt to disallow any other input, (especially any back-door attempts), I've got the following:

$number =~ /\d{1,2}/;
$number != 0;
$number < 11;

Is there a way to write this as one, separate regex?

To clarify, only a number from 1 to 10 can be entered from a select box in the script.

Anyone?
 
Maybe this will give you a direction to go in:

Code:
$number =~ /^[1-9][0]?$/;
 
yes, Thanx, rharsh, for the directional... now, I suppose I've got to rethink the $number < 11 bit, since, that is the part I can't seem to fiddle with correctly...
 
Actually, I think maybe that will work, as is...
I tried the following:

Code:
#!/usr/bin/perl

use strict;
use CGI qw/:standard/;

my @numbers = qw/1 2 3 4 5 6 7 8 9 10 99 56 34 32 87 00 11 209 008/;

print header;

    for (sort(@numbers)) {
        if ($numbers[$_] =~ /^[1-9][0]?$/) {
            print "$numbers[$_] is OK\n";
        } else { print "$numbers[$_] is NO GOOD\n"; }
    }

and it produced the following:

Code:
1 is OK
9 is OK
2 is OK
99 is NO GOOD
56 is NO GOOD
3 is OK
 is NO GOOD
4 is OK
 is NO GOOD
 is NO GOOD
5 is OK
6 is OK
 is NO GOOD
7 is OK
8 is OK
9 is OK
 is NO GOOD
10 is OK
 is NO GOOD




 
$_ inside your for loop is the current element of @numbers, not an index as you seem to think. You're getting lines that say, "[blank] is no good" because you're using the value of the current element (e.g. 99) as an index into the array, referencing elements that don't exist. Also, I guess you want to sort the list numerically.
Try this:
Code:
for (sort {$a <=> $b} @numbers) {
    if (/^[1-9][0]?$/) {
        print "$_ is OK\n";
    } else { 
        print "$_ is NO GOOD\n"; }
}

[b]Output:[/b]
00 is NO GOOD
1 is OK
2 is OK
3 is OK
4 is OK
5 is OK
6 is OK
7 is OK
8 is OK
008 is NO GOOD
9 is OK
10 is OK
11 is NO GOOD
32 is NO GOOD
34 is NO GOOD
56 is NO GOOD
87 is NO GOOD
99 is NO GOOD
209 is NO GOOD

This seems to do what you want.

 
The re /^[1-9][0]?$/ has problems, though.
This will never allow a leading zero, but will allow 20, 30, 40, ... up to 90. For example, with
my @numbers = qw/20 30 40 50 06 07 08 90 01/;,
you get the following output

01 is NO GOOD
06 is NO GOOD
07 is NO GOOD
08 is NO GOOD
20 is OK
30 is OK
40 is OK
50 is OK
90 is OK


which I don't think is what you intend.
How about /^(0?[1-9]|10)$/ instead? This gives you

01 is OK
06 is OK
07 is OK
08 is OK
20 is NO GOOD
30 is NO GOOD
40 is NO GOOD
50 is NO GOOD
90 is NO GOOD

which I think is what you want.
 
Great reply, mikevh, as you pointed out, I applied incorrect usage in my example. Yours, fortunately showed the problems that the original regex potentially contained, by using 20, 30, 40... etc. So, I think I'm all set. Even if a number were to be 08, 05...etc, it would not affect the datafile storing the results.

ThanX
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top