reg exp question

K

kstinson00

I have the following logic in a perl script:
if (! $Department =~ m/^0*(\d){1,3}$/ ) {
$Department = "000";
}


The intent was to find any value of $Department that was not 1 to 3
digits excluding leading 0's and set the var to "000" else the value
just be as is. In other words this is a basic "edit" on the department
field.

The above does not seem to work.

Your help is appreciated.

Kevin
 
M

Matt Garrish

I have the following logic in a perl script:
if (! $Department =~ m/^0*(\d){1,3}$/ ) {

Which is more cleanly written in Perl as:

if ($Department !~ /^0*\d{1,3}$/) {

Your problem has to do with precedence. You negate $Department and then
match against it. You want to negate the return value of the pattern match.
To fix your code you need to add another set of parens:

if (! ($Department =~ m/^0*(\d){1,3}$/) )

But that's getting even uglier (not to mention the useless capture in the
regex), so I'd strongly recommend the version above.

Matt
 
M

Matt Garrish

Matt Garrish said:
Which is more cleanly written in Perl as:

if ($Department !~ /^0*\d{1,3}$/) {

Your problem has to do with precedence. You negate $Department and then
match against it. You want to negate the return value of the pattern
match. To fix your code you need to add another set of parens:

if (! ($Department =~ m/^0*(\d){1,3}$/) )

Forgot to mention that you can change the conditional as well:

unless ($Department =~ m/^0*(\d){1,3}$/) {

But again, capturing the digits when they aren't needed is wasteful.

Matt
 
J

J. Gleixner

I have the following logic in a perl script:
if (! $Department =~ m/^0*(\d){1,3}$/ ) {
$Department = "000";
}


The intent was to find any value of $Department that was not 1 to 3
digits excluding leading 0's and set the var to "000" else the value
just be as is. In other words this is a basic "edit" on the department
field.

The above does not seem to work.

Your help is appreciated.

Kevin

You want to negate the match, instead of what the match returns.

if ( $Department !~ m/^0*(\d){1,3}$/ ) {

It might be a bit more readable to do what you want by not using regular
expressions:

$Department = 0 unless $Department>=0 and $Department<1000;

Change it to "000", when it's needed (sprintf or other methods),
otherwise just use 0.
 
J

J. Gleixner

J. Gleixner said:
You want to negate the match, instead of what the match returns.

if ( $Department !~ m/^0*(\d){1,3}$/ ) {

It might be a bit more readable to do what you want by not using regular
expressions:

$Department = 0 unless $Department>=0 and $Department<1000;

Correction:

$Department = 0 unless $Department>0 and $Department<1000;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top