SPUG: question regarding using reg exp in "if" statements.

Rick Brannan rick at libronix.com
Fri Sep 10 17:22:47 CDT 1999


Instead of ... 

> if($string eq /\s/)
> if($string ne /\w/)
> if($string eq m/\s/)
> if($string ne m/\w/)

How about ... 

> if($string =~ /\s/)
> if($string =~ /\w/)
> if($string =~ m/\s/)
> if($string =~ m/\w/)

That oughta do the trick. You're not seeing if they're equal, you're doing a
regex compare, so the =~ is the proper beast for the trick. However, the
above would just check to see if a space character exists in $string, not if
that's all it has. Maybe you'd be better off with something like:

> if($string =~ /^\s*$/)

The perlre page has got groovy info, as does Jeffrey Friedl's massive
"Mastering Regular Expressions" from O'Reilly, which is the regex bible.
Highly recommended.
______________________________
Rick Brannan -- Libronix Corp.
rick at libronix.com -- http://www.libronix.com
Parlez vous Supakoo? http://www.supakoo.com

"You can't lose what you ain't never had."
 -- Muddy Waters


> -----Original Message-----
> From: Daniel V. Ebert [mailto:debert at dusya.osd.com]
> Sent: Friday, September 10, 1999 2:16 PM
> To: spug-list at pm.org
> Subject: SPUG: question regarding using reg exp in "if" statements.
> 
> 
> 
> I have written an "if" statement:
> 
> if ($string eq "") { whatever;}
> else{ something else;}
> 
> to check for blank entries in a form CGI.  This works except 
> that " "  
> doesn't satisfy the if statemenet condition.
> 
> What I really want is (in plain English) is:
> if ($string is only white space)
> 
> I've tried stuff like:
> 
> if($string eq /\s/)
> if($string ne /\w/)
> if($string eq m/\s/)
> if($string ne m/\w/)
> etc.
> but they don't work.  I'm probably missing something obvious, 
> but I can't  
> figure it out.  Any help would be appreciated.  Thanks.
> 
> ---
> Dan Ebert <debert at seanet.com> or <mathin at mathin.com>
> Seanet Internet Services
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - - - - - -
>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: 
http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list