Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
undef(); and delete();



sleuth
Enthusiast

Dec 12, 2000, 1:45 PM


Views: 43296
undef(); and delete();

$hash{'key'} = "value";

What is the difference between undef($hash{'key'}); and delete($hash{'key']); ?

Sleuth



perlplexer
Deleted

Dec 13, 2000, 6:13 AM


Views: 43284
Re: undef(); and delete();

1) delete($hash{'key'}); - deletes both the key and the value from %hash returning the deleted value.

exists($hash{'key'}) returns false

2) undef($hash{'key'}); - simply undefs the value that is associated with 'key'

exists($hash{'key'}) returns true



sleuth
Enthusiast

Dec 13, 2000, 12:06 PM


Views: 43275
Re: undef(); and delete();

 
Perfect Score!!!! You hit it right in the mark, that's exactly what I was getting at.

If you undef the hash key, exists($hash{'key'}); returns true.

Great job perlplexer,

Sleuth