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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables that are null or contain no data. 1

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
I am trying to see if a variable has any data in it, how can I see if a variable is null or does not contain any data.

Thank you,

Tim Gallagher


-How important does a person have to be before they are considered assissinated instead of just murdered?

 
you can use the 'eq' sign to compare variables and their contents, for example:
Code:
if ($variable eq "")  {
    print "Variable is empty\n";
}  else  {
    print "Variable is not empty\n";
}

It looks like you're maybe a newbie so a good resource for perl can be found here:
Rob Waite
 
Thanks, I am a newbie to perl, comming from a VB/WMI background.

Timgerr

-How important does a person have to be before they are considered assissinated instead of just murdered?

 
If you want to check if a variable is defined, use the aptly-named defined function:
Code:
if (defined $variable && $variable ne "") {
   #variable is defined and not empty
}
else {
   #variable is undefined or empty
}

waiterm's code will throw a warning if $variable is undefined.
 
If you're lazy - you can just type

if($variable){
print "is defined and has a value\n";
} else {
print "is undefined or has no value\n";
}


Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Mike, if $variable has a value of zero, wouldn't that also print "is undefined or has no value"?

 
[blush] It might do...

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top