For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2005 > reg exp question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reg exp question
kstinson00@gmail.com

2005-12-16, 7:03 pm

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

Matt Garrish

2005-12-16, 7:03 pm


<kstinson00@gmail.com> wrote in message
news:1134768103.452275.60890@g43g2000cwa.googlegroups.com...
>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


J. Gleixner

2005-12-16, 7:03 pm

kstinson00@gmail.com wrote:
> 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. Gleixner

2005-12-16, 7:03 pm

J. Gleixner wrote:
> kstinson00@gmail.com wrote:
>
>
> 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;
Jim Gibson

2005-12-16, 7:03 pm

In article <1134768103.452275.60890@g43g2000cwa.googlegroups.com>,
<kstinson00@gmail.com> wrote:

> 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.


The ! operator has a higher precedence than the =~ operator, so you are
trying to match the expression (! $Department). Use the negated binding
operator !~ instead:

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

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Matt Garrish

2005-12-16, 7:03 pm


"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:awGof.3790$El.366787@news20.bellglobal.com...
>
> <kstinson00@gmail.com> wrote in message
> news:1134768103.452275.60890@g43g2000cwa.googlegroups.com...
>
> 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


Tad McClellan

2005-12-16, 9:57 pm

kstinson00@gmail.com <kstinson00@gmail.com> wrote:

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



$Department = "000" unless $Department =~ m/^0*\d{1,3}$/;


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com