View Full Version : Checking for empty string
Hi,
How can I check for empty string?
I tried using:
$string = "";
if($string eq ""){print "empy";}
but if $string = " " or " ";
this will not work.
Any help is appreciated.
mlseim
07-08-2005, 02:33 PM
How about the reverse of "if" which is, "unless" ...
unless($string){
#do this if $string is null;
}
For the example you gave, you need to remember that strings and integers get compared differently. Integers: == (equal) Strings: eq (equal)
For Integers:
if($number == 12){
# do this if $number is 12.
}
For Strings:
if($string eq "codingforums"){
# do this
}
====================
EDIT:
And now I see that I misunderstood your original question.
A "space" is actually a character, so the $string is not empty (or null).
$string = " "; #this is a space.
$string = ""; #this is null.
You could filter out all of the spaces just for checking the $string.
$FILTER allows a-z, caps A-Z, numbers 0-9 ... everything else is removed.
$FILTER = "a-zA-Z0-9";
$test = $string;
$test =~ s/[^$FILTER]//go;
if($test){
#do this here, since $test is NOT null, there must be more
#characters than just a space.
}
.
have you tried
if (!string) {
the string variable is empty;
} else { there's something in the string
}
might work for you as it has for me.
Bazz
These answers are so lame I was forced to sign up for the site just to post this...
perl -ae '$a = "\t \n"; print "hello world!\n" if ($a =~ /^\s*$/);'
ie. if $a contains nothing (the asterisk,* takes care of this), or any other space characters (\s), from beginning (^) to end ($).
jmh0
oesxyl
10-04-2008, 02:16 AM
These answers are so lame I was forced to sign up for the site just to post this...
perl -ae '$a = "\t \n"; print "hello world!\n" if ($a =~ /^\s*$/);'
ie. if $a contains nothing (the asterisk,* takes care of this), or any other space characters (\s), from beginning (^) to end ($).
jmh0
why did you use -a?
regards
KevinADC
10-04-2008, 02:47 AM
These answers are so lame I was forced to sign up for the site just to post this...
perl -ae '$a = "\t \n"; print "hello world!\n" if ($a =~ /^\s*$/);'
ie. if $a contains nothing (the asterisk,* takes care of this), or any other space characters (\s), from beginning (^) to end ($).
jmh0
Yea, and they were lame three years ago when they were originally posted. ;)
oesxyl
10-04-2008, 02:50 AM
Yea, and they were lame three years ago when they were originally posted. ;)
I miss that, :)
best regards
FishMonger
10-04-2008, 03:15 AM
jmh0,
No one forced you to do anything, including signing up and responding to a 3yr old thread.
Your answer isn't as good as you think. Firstly, you used the -a switch for absolutely no reason and secondly, testing the length would be more efficient than the regex.
perl -e '$a = "\t \n"; print "hello world!\n" if length $a;'
KevinADC
10-04-2008, 04:44 AM
jmh0,
No one forced you to do anything, .....
Maybe he was kidnapped at gun point and forced to sign up. :eek:
MohitVohra
11-04-2010, 11:52 AM
I wanted to say thanks to bazz as I've tried bazz's suggestion and it worked wonders! I don't know about other versions of perl, I'm using ActivePerl 5.8 and it worked well on that, so I'm pretty satisfied with this answer.
Thanks a lot bazz for your answer and hope I didn't offend anyone by my comments.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.