Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find if a string is null 3

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi i am trying to go through a content of a file and check for null character...but its not working--

$htmlcontent = "";
if ($html eq "0"){
# to check for null char i use:
if ($char ne " "){
$htmlcontent = $htmlcontent . $char;
}
}
anybody have ideas as to what else will work???
ko12
 
Well, to check for a non-null string would be:

Code:
if (!$string)
{
    #process here
}

Not sure what you mean by a null character. Are you specifically looking for a space or tab or something as the null value?

- Rieekan
 
If you want to check for empty then you musn't have a space between the quotes.

Perl has a built in function that when you ask

Code:
if($varname ne ""){
}

or

if($varname eq ""){
}

if $varname is NULL or has no value it is conveted to an empty string first and then evaluates against "".

"" is NOT NULL it is an EMPTY STRING.

hope that makes sense
 
hi,
thanks for replies.
by null --i meant empty spaces.
i tried both
if($varname ne ""){
}
and
if (!$string)
but they did not work---any ideas??
thanks
 
if ( $variable =~ /^$/ ) {
print "Variable is null\n";
}

if ( $variable !~ /^$/ ) {
print "Variable is not null\n";
}



dmazzini
GSM System and Telecomm Consultant

 
To look for a string that only has spaces, you can do the following, but it will check for any whitespace character (tab, carriage return, enter, space)

Code:
 if ( $variable =~ /^\s$/ ) {
      print "Variable is null\n";
  }

- Rieekan
 
Just a slight alteration to Rieekan's code to allow for any number of spaces, rather than just one:
Code:
 if ( $variable =~ /^\s*$/ ) {
      print "Variable is null\n";
  }
 
Oops, good catch ishnid. It's just too early in the morning to be thinking.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top