On Friday 09 July 2004 19:25, Gunnar Hjalmarsson wrote:
> Philipp Traeder wrote:
> > I don't know what an expert would say,
>
> Neither do I. :)

I think this is a good base for talking about hashes. ;-)

> > but for testing if a hash contains an element, I normally use
> > "exists":
>
> Your wording "if a hash contains an element" is ambigous IMO. A hash
> or associative array consists of key/value pairs, where the keys are
> indexes and the values are considered to be the elements (I think).

Ok - that's a possible terminology, and yes, I wasn't completely precise ;-)

>
> >   if (exists ($hash{'mykey'})) {
> >     print 'mykey exists.';
> >   }
>
> That's fine, as long as you are aware that $hash{mykey} may well be
> undefined even if it "exists".

I think *this* is the interesting point here - it's quite possible that a 
certain key exists in a hash, but has an undefined value:

  my %hash = (42 => undef);

  print "42 exists\n" if (exists $hash{42});
  print "42 is defined\n" if (defined $hash{42});

If you ask me, you can't put up a general rule about using "exists" or 
"defined" - it's a questions of how/on which conditions the hash has been 
filled...

Just my 5 cent,

Philipp

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to