Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Handling undefined string content

by akm2 (Scribe)
on Jan 31, 2001 at 22:36 UTC ( [id://55523]=perlquestion: print w/replies, xml ) Need Help??

akm2 has asked for the wisdom of the Perl Monks concerning the following question:

How do you tell perl to do somthing if a $var only contains spaces when you dont know the length of $var?

Replies are listed 'Best First'.
Re: Handling undefined string content
by danger (Priest) on Jan 31, 2001 at 22:46 UTC

    Use a regex to see if the string contains only spaces (or whitespace, which also includes [ \t\r\f\n]):

    print "Spaces only\n" if $var =~ /^ +\z/; print "Only whitespace\n" if $var =~ /^\s+$/; print "Whitespace or empty\n" if $var =~ /^\s*$/;

    I used \z as the end of string anchor on the first one because you didn't specify whether the string was a line and a \n at the end might not be acceptable.

Re: Handling undefined string content
by arturo (Vicar) on Jan 31, 2001 at 22:40 UTC

    Use a regular expression:

    if ($var =~ /^\s*$/) { # do something }

    The regex means "if $var consists of nothing but 0 or more spaces between the beginning and the end ..." (for more on that, type perldoc perlre on your system)

    Beware : defined has a meaning in Perl.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Handling undefined string content
by Trinary (Pilgrim) on Jan 31, 2001 at 22:42 UTC
    if ($var =~ /^\s+$/) { #do something. }
    Explanation: the =~ makes a regular expression bind against a var, instead of $_ / / delimit the regexp. ^ denotes the beginning of a string, $ the end. \s+ means one or more whitespace characters, which is space, \n, \t, \v, or \r. So the whole thing means: do something if, in this var, there's nothing but whitespance between the beginning and end of the contents.

    Check out perlre. Regular expressions are good.

    Enjoy

    Trinary

Re: Handling undefined string content
by jeroenes (Priest) on Jan 31, 2001 at 23:00 UTC
    if (length(substr($var, ' ')) eq length( $var)) {} might be considered as well.

    Jeroen
    "We are not alone"(FZ)

    Update Read lemming's reply. Forget the substr approach. Luckily, I thought of another approach on the way home:

    if( $var eq ' ' x length($var) ) {...}
    Should do the trick without regex or functions.

    Yet another would be to use tr:

    if( $var eq map tr/A..Za..z0..9/ /, $var ) {..}
    or make that
    if( $var eq map tr/\000..\277/ /, $var ) {..}
    to be safe.

    Update2: lemming's update hits it once more. I confused the foreach behavior with map's. Moreover, I should have evaluated in array context. Well, you get this:

    if($var eq join '', map{ tr/\000..\177/ /c; $_; } split '', $var ) { +...}
    This is tested and it works. Note the use of c to complement. I wouldn't use this myself, though.
      Drat. jeroen took off just as I spotted his possible typo.
      Hopefully, he'll correct it.
      if (length(substr($var, index($var, ' '), rindex($var, ' ')+1)) == len +gth($var) { Dosomething(); }

      Not that I'd advocate that approach. The rindex+1, could have length instead, but I still wouldn't use it over the regex answers

      On to what's wrong with jeroen's statement: sorry
      substr($var,' ') will always return the full string since ' ' will be interpreted as 0.
      use warnings will point out not a numeric argument.
      Also since length returns numeric values, == instead of eq should be used. Not that this would happen, but "010" eq "10" is false while "010" == "10" is true.

      Update: Ah good. He saw it, the length x space works, the tr solution does not. tr returns the number of matches to a scalar. So if you had 10 spaces in your string, you would get the number 10.
      And of course these would only we applicable to space answers, not on any whitespace.

Re: Handling undefined string content
by Fastolfe (Vicar) on Feb 01, 2001 at 03:24 UTC
    If you're going to go the regex approach, I might test for the presence of a non-whitespace character instead of looking to see that the string is composed entirely of whitespace. Generally this will be faster:
    if ($string !~ /\S/) { # $string is empty or composed entirely of spaces }
    You might use length and/or the "trueness" of the string if you want to test if it has content at all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://55523]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found