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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for a 4 digit number 2

Status
Not open for further replies.

daveryan

Programmer
Joined
Sep 1, 2005
Messages
9
Location
GB
I'm having problems with a perl script I'm working on for some course work.

The script asks for a four digit number before going onto the next part of the script.

The problem is I need the script to only accept a four digit number but whatever I try I can usually get it to accept three figures and sometimes five or more figures.

I've tried using the IF statement, the WHILE statement, the UNTIL, DO WHILE, etc....but still I can't solve this problem.

Here is the script using an if statement:


#!c:\perl\bin\perl.exe

print("Enter your four digit customer reference number.\n");
$ref=<STDIN>;

if ($ref !~ m/[0-9]{4}/)
{
print("Not a valid number, please enter your correct customer reference number.\n");
$ref=<STDIN>;
}



Can anyone help me?


Dave
 
We're not supposed to help you with school work but as a hint, you need to make your regex match the start of the string, the digits and then the end so that nothing else can be there.




(slowly healing) Trojan.
 
Hi Dave

As you are doing this for course work i will try to point you in the right direction - but not go ahead and solve the problem

Your script is just missing a very small requirement. All the regex is checking is that whatever is keyed in has 4 digits in succession. It doesn't check to see if the number starts at the beginning of the line or that the last digit is at the end of the line (HINT)

At present all of the following would be accepted

abc1234567def
3456blahblahblah
xyz1234

Despite your comment in your post - your script will not accept any LESS than 4 characters - so that bit is tied up

I have got to go out for the day now but i am sure you will get what i mean


Kind Regards
Duncan
 
Hi Trojan

Hope you are well

You are into your bikes - aren't you? Just bought a second-hand YZF 750R the other day. Might not be posting replies much longer!...


Kind Regards
Duncan
 
Thanks for the replies,

I'm not at school, I'm 40 years old, although it is course work.

The script does not accept a 3 digit number the first time of asking, it tells me the number is invalid, but it will accept a 3 digit number the second time.

I'm guessing I need to put a ^ for the begining of the line and a $ for the end of the line, sure I've already tried this but I'll have another go.

Thanks, I'll post my results.
 
OK, this is now my code, it works the first time but will accept 3 and 5 digit numbers the second time.

#!c:\perl\bin\perl.exe

print("Enter your four digit customer reference number.\n");
$ref=<STDIN>;

if ($ref!~m/^[0-9]{4}$/)
{
print("Not a valid number, please enter your correct customer reference number.\n");
$ref=<STDIN>;
}
 
Cool. First part sussed. Program flow... what makes you think it is suddenly going to jump back to the top of the script after an attempt?


Kind Regards
Duncan
 
sorry - i.e. it will actually accept ANYTHING the second time... because it is not running the script again. It is just in limbo.


Kind Regards
Duncan
 
Right, I think I need to put it in a loop not the if statement.
Trying that.
 
Tried the while loop and it seems to work, thanks for the help, I think you'll be seeing more of me.

Sure I tried this yesterday and didn't work, must have missed something.

Here's the code:

#!c:\perl\bin\perl.exe

print("Enter your four digit customer reference number.\n");
$ref=<STDIN>;

while ($ref!~m/^[0-9]{4}$/)
{
print("Not a valid number, please enter your correct customer reference number.\n");
$ref=<STDIN>;
}

Thanks guys, your stars.
 
Code:
[b]#!c:\perl\bin\perl.exe[/b]

print "Enter your four digit customer reference number.\n";

while (<STDIN>) {
  if (/^[0-9]{4}$/) {
    print "nice one\n";
    exit;
  } else {
    print "Not a valid number, please enter your correct customer reference number\n";
  }
}

...


Kind Regards
Duncan
 
Good boy duncdude!

Yep, I'm into bikes.
Had a 1998 R1, a 2000 R1, a 2002 GSX-R1000 and a 2003 GSX-R1000 but without a bike at the mo. :(

Hope you have lots of fun, wish I could join you!




(slowly healing) Trojan.
 
Hi Trojan. Those R1's are mental - i have a couple of friends who have had 180 mph on those space ships. My YZF is plenty fast enough thanks. I'm just glad your injury wasn't due to one of those crazy beasts!


Kind Regards
Duncan
 
I've had a good injury or two from them, trust me!
But I hate being without them.
Anyway, nice to see you keeping the biker Perl end up!




(slowly healing) Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top