perl hash functions

They have: 117 posts

Joined: Feb 2000

Hello,

Does anyone know how to use the Perl hash function "exists"?
The book I'm using says there is a hash function as follows:

exists $hash{$key} which returns true if the key is found in the hash table.

I want to do something like:

if (exists $hash($key})
{do something ...}
else
{do something else...}

I keep getting errors. Does anyone know the proper syntax for this function? Thanks.

Bob

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I don't know, never used it. Found these links though, hope they help...

http://www.perl.com/pub/doc/manual/html/pod/perlfunc/exists.html
http://www.perl.com/pub/doc/manual/html/pod/perlfunc/defined.html

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 161 posts

Joined: Dec 1999

You wrote:

if (exists $hash($key})
'

which has a syntax error. Perhaps you meant

if (exists $hash<strong>{</strong>$key})
'

Regardless, the exists function should not give you errors. Your problem is elsewhere -- perhaps you're using an older version of Perl?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.