http://www.perlmonks.org?node_id=4685

vroom has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How do I completely remove a key/value pair from my hash?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I completely remove a key/value pair from a hash?
by vroom (His Eminence) on Jan 21, 2000 at 01:38 UTC
    use delete
    #Removes key and its associated value delete $hash{key}; #gets rid of a slice of key/value pairs at once; delete @hash {"first","second","third"};
Re: How do I completely remove a key/value pair from my hash?
by chromatic (Archbishop) on Mar 06, 2000 at 08:54 UTC
    Note that undef (Which many people try first) is the wrong answer. It only sets the value associated with a key to undef. That is usually not what you want:
    my %hash = ('one' => 'two', 'two' => '2'); undef $hash{'one'}; print keys %hash;
    This prints: twoone. Adding: print values %hash; prints: 2.