2009/3/17 Jay Savage <daggerqu...@gmail.com>: > On Tue, Mar 17, 2009 at 8:16 AM, Dermot <paik...@googlemail.com> wrote: >> Hi All, >> >> I am unsure if the following statement is going to do what I want. >> >> I want to test if there is a value in $hash_ref{'someval'} NOT if the >> key is defined. I'd also like to avoid un-sightly "undefined value" >> errors. > > > Hi Dermot, > > It really depends on what the expected/legal values are. > > For numeric values, a simple 'if $x{x}' will normally suffice. The > exception is in the case that zero is a legal value, in which case you > need 'defined.' The perldoc exists for a nice rundown. > > If there are additional constraints, you really need at least two > tests. Simply testing truth or definiedness will not tell you if the > value is in range, and simply testing the value will throw an error if > the element isn't defined, e.g. > > if ( $x{x} and $x{x} =~ /\w+/ ) {} > if ( defined $x{x} and $x{x} > -4 ) {} # 0 is in range, so we > need 'defined' >
Thanx for the replies. I have opted for the defined option as I think it makes the code fragment's intention clearer but just to confirm if ($x{$x}) inspects the key's value, not simply the existence of a key? Dp. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/