Gossamer Forum
Home : General : Perl Programming :

Correct syntax?

Quote Reply
Correct syntax?
Hi everyone,

I need a little help on the correct Perl syntax for the following:

I am trying to check for a form entry of at least 3 digits, but the following statement results in errors?

unless ($in{'ID'} =~ \\d{3}$\) { # checks that the ID field is a number of at least 3 digits
&html_view_failure("Please specify a valid Property ID Number");
return;
}

Quote Reply
Re: Correct syntax? In reply to
Sorry! and I should have persisted more before posting!!!

As a newbie to Perl I don't know my forward slash from by back slash:

unless ($in{'ID'} =~ /\d{3}$/) { # checks that the ID field is a number of at least 3 digits
&html_view_failure("Please specify a valid Property ID Number");
return;
}

worked perfectly!

Quote Reply
Re: Correct syntax? In reply to
In Reply To:
I am trying to check for a form entry of at least 3 digits, but the following statement results in errors?

unless ($in{'ID'} =~ \\d{3}$\) { # checks that the ID field is a number of at least 3 digits
&html_view_failure("Please specify a valid Property ID Number");
return;
}
That regex doesn't really work as you want... You probably want it to be something like:
Code:
/^\d{3,}$/
instead...
Your one would match 'abc123', etc.

Adrian
Quote Reply
Re: Correct syntax? In reply to
Thanks for the info.

Excuse my ignorance - I know what the ^ does, but what does the , after the 3 do?

Quote Reply
Re: Correct syntax? In reply to
There's a regex help file on your system. From a command line type perldoc perlre and that will contain all the info you need.

Quote Reply
Re: Correct syntax? In reply to
Code:
/^\d{3,}$/
^ matches the beginning of the string
$ matches the end of the string
\d is any digit
{n,} is match n or more times
{n} is match exactly n times
{n,m} is match n or more times, but not more than m times
...

go look for a regex tutorial or read some docs for info on other regex... Section 2.4: Pattern Matching, of Programming Perl is a good start.

Adrian