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

Hash Value is defined or not null 1

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I want to verify if a value (alphanumeric value) is defined (no null/blank value) and I can't seem to succeed even though it's simple.

Code:
# Value: Phone is (222) 222-2222 

my $Value = $Hash->{'Value'};

if ($Value ne '') {
    print "Value is defined";
}

I have tried to use the DEFINED function, but it's not reliable with hashes.

Any help would be appreciated

 
The defined() function is perfectly reliable. It must do something other than what you're expecting.

The code you've posted above should work fine by itself if you don't have warnings turned on. If you do, you can get rid of the "uninitialised value" warning with:
Code:
if ( defined( $Value ) and $Value ne '' ) {
   print "Value is defined";
}
 
you can try using the exists function:
Code:
 if (exists($Hash->{'Value'})) {}

"I have tried to use the DEFINED function, but it's not reliable with hashes"... haha :)

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Ok. It's still not working even by using DEFINED and NOT EQUAL ' '.

I will post the complete code in few minutes. This is quite bizarre. It's within a foreach loop, so something must wrong with my loop

133tcamel
EXISTS has a different purpose ?? It will return a true value if the key exists; but I need to know if the value is defined because sometimes the value can be null / blank.

Another related question;
To verify if a STRING is define or not null, what is the best technique ? The string is alphanumeric.

1. if $string ne ' ';
2. if $string gt 0;

???

Thanks again
 
HI, Extension

1. if $string ne ' ';
2. if $string gt 0;

3. if $string !~ /^\s*$/

one more option.;-)
 
eewah

Your pattern matching exp is working perfectly. Still don't know why the ne '' doesn't work. Weird issue !
I guess I will have to stick with the pattern mat. exp. even though it's not super clean.
 
First check if the has key exists. If it does, it can still have a value of 'undefined'.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
For 'has' read 'hash'... [blush]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Let me discuss a special case as a pointer.

Sometimes when you are trying 'exists()' on a tied hash, older versions of AnyDBM_File don't allow that....the exists() function isn't even defined in the module for tied hashes.

But the latest versions do allow...

Regular expressions are the best tool because a simple regexp works on all OS, all webservers, whatever whatever... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top