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.
- Variable has never been given a value.
Test with unless (defined $foo)
- 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.)
- 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
| [reply] |
|
|
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/
| [reply] [d/l] [select] |
|
|
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
| [reply] |
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.) | [reply] [d/l] [select] |
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 | [reply] [d/l] |
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.
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
|
|
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
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
(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 | [reply] |