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!

Numbers Question 1

Status
Not open for further replies.

rdyoll

Technical User
Aug 11, 2003
39
US
I'm looking for a regular expression that will allow any number from 0 - 100, but there are some "numbers" that cannot be used. I have a maxlength input of 3 digits, numbers only.

Here is the general break-down:

numbers, only 0-9
100 is highest - so the only 3 digit number is 100
0 is lowest
number cannot start with 0, if more than 1 digit, like 032
number cannot start with 00, like 004, but can be 4
number CAN be 0
number can be one digit, like 1 or 9, not 01 or 09
number can be 2 digit, like 47 or 86, not 047 or 086
here is what I have so far, but is FULL of holes!

$number =~ /^[0-9]*$/g
$number <= 100
$number =~ s/^[0]*//g <- this screws things up, some

I've tried alot of different combinations, but none are exact. Can anyone see a simple solution?

Thanx!

 
Do you want to reject '07' as input, or would it be okay to accept that but change it to '7'? There's much easier ways to do that, just check if it's numeric and within your range, then force numeric context. Something like this:
Code:
if($number =~ /^\d+$/ && $number <= 100)
{
    $number += 0;
    #now $number is in the acceptable format
}

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
If that's not the case, something like this appears to work, but is more convoluded:
Code:
for(qw/100 032 004 0 01 09 47 86 047 086 733/)
{
	# check if one to three digits and less than 100
	if(/^\d{1,3}$/ && $_ <= 100)
	{
		# if is starts with zero, it better be '0', else skip
		next if(/^0/ && $_ ne '0');
		print &quot;$_\n&quot;;
	}
}

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
thanx, I'll run them both and see the difference...I'm sure either will work...back later..
 
icrf...
the second block of code you provided does exactly as I need. My problem, now, is how to implement that into my script. I don't know how to use the next if function if I'm not actually printing. Here is the beginning snippet of my original script. How do I integrate your code into mine?

my $q = CGI->new();
my $input = $q->param( 'input' );

my $cookie = $q->cookie( -name => $cookie_name );
my $server = quotemeta( $q->virtual_host || $q->server_name );

if (defined $cookie) {
error( $q, &quot;SORRY!&quot; );
}
elsif (($input =~ /^[0-9]*$/g &&
$input ne '' &&
$input <= 100) &&
($q->referer =~ m|^http?://$server/|) &&
($q->request_method eq 'POST')) {
if (open (FILE, &quot;+<$file_dat&quot;)) {
if (open (FILE2, &quot;+<$file2_dat&quot;)) {
flock (FILE2, LOCK_EX);
my $info = <FILE2>;
seek (FILE2, 0, SEEK_SET);
print FILE2 $data+$data2.&quot;\n&quot;;
truncate (FILE2, tell);
close (FILE2);
...................................
...................................
...................................

Thanx

 
next just breaks the flow of the loop and puts it at the start of the nearest loop (or one specified by label). In that example, it just skips the print statement. Check perldoc for the function's details. Maybe it'd be easier just to put it all in one, big, ugly conditional that you can put wherever you need to:
Code:
if( $input =~ /^\d{1,3}$/ && $input <= 100 && ($input !~ /^0/ || $input eq '0'))
{
    # $input is a valid number
}

# or a little prettier, maybe:

if( $input =~ /^\d{1,3}$/ && # 1-3 digits, and
    $input <= 100 &&         # value <= 100, and
    (
      $input !~ /^0/ ||      # does NOT start with zero, or
      $input eq '0'          # is equal to zero
    )
  )
{
    # $input is a valid number
}
The number it checks is in $input, if the number is valid, you enter the block of the if statement. From your sample code, I assume this can just be put in place of your elsif's conditional. Let me know if you have any problems.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
...I'm such a dummy, sometimes...

I rearranged the elsif conditional to END with your block of code...it worx sweet!!

elsif ( $q->referer =~ m|^http?://$server/| &&
$q->request_method eq 'POST' &&
$input =~ /^\d{1,3}$/ &&
$input <= 100 &&
(
$input !~ /^0/ ||
$input eq '0'
)
)
{

...............
...............
...............

Thank you for all your help

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top