|
|
Perl Hash Element delete() Function
|
Tutorials

Perl

|
Topic |
What is delete() Function in Perl programming?
|
|
Explanation |
|
The delete() function deletes a value or a hash completely.
Syntax:
delete hash;
Example:
#! C:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
%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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
%name = ('Tom',26,'Peter',51,'Jones', 23, 'John', 43);
foreach $key (keys %name)
{
delete $name{$key};
}
print "Values of Hash after deleting\n";
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.
|
|
A Note |
|
Simple introduction, basic CGI perl programming codes with examples.
Do send your feedback or suggestions on this tutorial.
This is a copyright content.
|
|
|
|