Perl Hash Element delete() Function

What is delete() Function in Perl programming?

Explanation

The delete() function deletes a value or a hash completely.

Syntax:


delete hash;

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
%color = ('1' => 'Green' , '2' => 'Orange',
'3' => 'Blue', '4' => 'Pink');
print %color;
print "<br>";
$del = delete($color{'3'});
print "The Hash after Deleting:n";
print "<br>";
print %color;
Result :

4Pink1Green3Blue2Orange
The Hash after Deleting:
4Pink1Green2Orange

In the above example the hash element "blue" is deleted and the rest of the hash elements are displayed.

Deleting a Hash:


Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
%name = ('Tom',26,'Peter',51,'Jones', 23, 'John', 43);
foreach $key (keys %name)
{
delete $name{$key};
}
print "Values of Hash after deletingn";
print scalar grep defined($_),values %name;
Result :

Values of Hash after deleting 0

In the above example a hash is deleted completely using a loop, then the hash is checked for any values and the result displays zero.

Ask Questions

Ask Question