Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

What is the best way to determine if a string is blank?

by fongsaiyuk (Pilgrim)
on Jan 03, 2001 at 21:25 UTC ( #49559=perlquestion: print w/replies, xml ) Need Help??

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

I my code I'm always making sure that string variables are set to a value. If they are set I do something, if they are blank, I do something else. Pretty typical.

I'm trying to increase my skillz to go beyond this type of checking:

if ($foo eq "") { #do something kool } else { #do something not so kool }

I'm thinking that a regexp could be used?
Thanks a lot for your insight!

fongsaiyuk

Replies are listed 'Best First'.
Re: What is the best way to determine if a string is blank?
by davorg (Chancellor) on Jan 03, 2001 at 22:05 UTC

    There are three potential levels of 'blankness' that you should be checking for.

    1. Variable has never been given a value.
      Test with unless (defined $foo)
    2. Variable contains an empty string.
      Test with if ($foo eq '') (note that you could simplify it to unless ($foo) if you could be sure that zero wasn't a valid value.)
    3. Variable contains only whitespace.
      Test with if ($foo =~ /\S/)

    Update Thanks to merlyn for pointing out the error in the second bullet point - which I've now corrected.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Re Bullet 3:
      my $foo = " bar"; if($foo =~ /\S/) { print "Whitespace only\n"; }
      I think you meant:
      my $foo = " bar"; if($foo =~ /^\s+$/) { print "Whitespace only\n"; }
      Update: Whoops, davorg is right (see below)... and easier to see. I was thinking \s instead of \S. Fixed above, but hokey, so use his code instead.
      --isotope
      http://www.skylab.org/~isotope/

        Actually it should have been unless ($foo =~ /\S/) to maintain the same logic as the other examples.

        I think you may be confusing \s (whitespace) with \S (non-whitespace).

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: What is the best way to determine if a string is blank?
by chipmunk (Parson) on Jan 03, 2001 at 21:38 UTC
    The best solution really depends on your definition of "blank". To check empty strings, $foo eq "" really is the simplest solution.

    If you also want to flag strings that contain only whitespace, a regex is appropriate: $foo =~ /^\s*$/ or $foo !~ /\S/.

    (In any solution, you may want to test with defined() first to avoid uninitialized value warnings.)

Re: What is the best way to determine if a string is blank?
by salvadors (Pilgrim) on Jan 03, 2001 at 21:47 UTC
    It mostly depends on what you want to happen under what circumstances.

    For example, do you want to do something 'kool' or 'not so kool' if $foo was "0"?

    In general you're probably not wanting to check just for an empty string, but for "truth".

    In that case you can then just do:

    if ($foo) { ... } else { ... }

    Tony

Re: What is the best way to determine if a string is blank?
by lemming (Priest) on Jan 03, 2001 at 21:35 UTC

    I don't see anything wrong with your construct and I would think a regex would make the code less readable in some circles. Depending on $/, /^$/ would check for an empty string as well. So if you're replacing blank variables you could do s/^$/Hi there/.

    I'm curious what the more articulate monks have to say.
Re: What is the best way to determine if a string is blank?
by EvanK (Chaplain) on Jan 04, 2001 at 00:06 UTC
    Well, personally, I'd use the defined() method, which returns false if it's passed a blank string, true otherwise:
    if ( ! defined($foo) ) { #do something kool } else { #do something not so kool }
    ______________________________________________
    It's hard to believe that everyone here is the result of the smartest sperm.
      That wouldn't do what is intended. Look at these cases...
      my $var1; my $var2 = undef; my $var3 = ""; my $var4 = 0; my $var5 = 1; my $var6 = "foo"; for ($var1,$var2,$var3,$var4,$var5,$var6) { print defined($_)?1:0 . "\t" . ($_)?1:0 . "\n"; } __END__ Output: 0 0 0 0 1 0 1 0 1 1 1 1
        yeah, i guess you're right, because it returned true with $var3...but as for $var4 and $var5, the ($_)?1:0 is checking if they're TRUE or FALSE, not whether they're defined. still, i get your point.

        ______________________________________________
        If the world didn't suck, we'd all fly off.
               -A friend of mine

(fongsaiyuk)Re: What is the best way to determine if a string is blank?
by fongsaiyuk (Pilgrim) on Jan 04, 2001 at 18:57 UTC
    Thanks again to all for your help.

    There is much to think on here beyond simply testing for "". ^-^

    It's nice to know that once in a while, a poor scribe's craft is not always doing A Bad Thing(tm) :)

    Thanks again.

    saiyuk

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2019-08-21 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?