Perl, Perl Tutorials

Delete Function in Perl with examples

“delete” function in Perl is mainly used to delete the given element/key, which in return deletes its associated value(s) too. So, exists() on such key would return false.
Note: Assigning undefined value to a key in has doesn’t remove its key but delete() function would do that. So, use delete() or assigning undefined value wisely.
The general syntax for delete function is:

delete EXPR

In list context, returns the value or values deleted, or the last such element in scalar context. The return list’s length always matches that of the argument list: deleting non-existent elements returns the undefined value in their corresponding positions.

delete() may also be used on arrays but its behavior is not guaranteed. Although exists() will return false for deleted entries, deleting array elements never changes indices of existing values. So, better, use shift() or splice() for such requirements. However, if all deleted elements fall at the end of an array, the array’s size shrinks to the position of the highest element that still tests true for exists(), or to 0 if none do.


WARNING: Calling delete on array values is deprecated and likely to be removed in a future version of Perl.
Note: Deleting from %ENV modifies the environment. Deleting from a hash tied to a DBM file deletes the entry from the DBM file.

Question:  How do I completely remove a key/value pair from a hash?
Answer: use delete function

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (1=>'one',2=>'two',3=>'three');
print Dumper \%hash;
delete $hash{2};
print Dumper \%hash;

Output would be:
$VAR1 = {
‘1’ => ‘one’,
‘3’ => ‘three’,
‘2’ => ‘two’
};
$VAR1 = {
‘1’ => ‘one’,
‘3’ => ‘three’
};

Question: How to get rid of more than one key/value pairs at once?
Answer: use slice technique which is nothing but a list of values (hint array). Use all such keys which you want to delete  in a sliced manner with delete function as shown here:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (1=>'one',2=>'two',3=>'three');
print Dumper \%hash;
delete @hash{1,2};
print Dumper \%hash;

Output would be:
$VAR1 = {
‘1’ => ‘one’,
‘3’ => ‘three’,
‘2’ => ‘two’
};
$VAR1 = {
‘3’ => ‘three’
};

As I said above also, don’t use undef in any such case, if you have to delete key/value pair(s) from hash, else it would result mismatched key/value pair(s).
Note that undef (Which many people try first) is the wrong answer. It only sets the value associated with a key to undef. You will still have same key/value pair(s) as earlier.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (1=>'one',2=>'two',3=>'three');
print Dumper \%hash;
undef $hash{1};
print Dumper \%hash;

Output would be:
$VAR1 = {
‘1’ => ‘one’,
‘3’ => ‘three’,
‘2’ => ‘two’
};
$VAR1 = {
‘1’ => undef,
‘3’ => ‘three’,
‘2’ => ‘two’
};

Tips: Although assigning undef can be useful, where we need to keep the key(s) but wish to clear value(s) , like simulation of issuing  library card to students. Student is a key and value may exists or can be under if that guy has not been issued any book by that time.