The Perl delete function removes a key-value pair from a hash. You can delete an individual element of a hash or a hash slice.In a list context, the delete function returns a list with the values deleted and in scalar context it returns the value corresponding to the last element deleted.
The syntax form of the Perl delete function is as follows:
Please note that calling delete on an array is deprecated and could be removed in the future versions of Perl. But in the 12.1 version of Perl is still available so we’ll stick around it a bit.
The delete function on an array means to delete the value of a particular index – it is not removing an element but instead it replaces the element with undef.
In the following example we use the delete function within a foreach loop to delete the values of the even elements of an array.
See the following example:
# create an array
my @array = (1..10);
foreach (0..$#array) {
delete $array[$_] if $_ % 2;
}
# print the array
foreach (0..$#array) {
print $array[$_], ' ' if exists $array[$_];
}
print "\n\n";
use Data::Dumper;
print Dumper(\@array);
This code produces the following output:1 3 5 7 9
$VAR1 = [
1,
undef,
3,
undef,
5,
undef,
7,
undef,
9
];
A few words about this code snippet:- @array contains the numbers from 1 to 10 – we used the range operator to create the array
- the first foreach loop:
- $#array is the index of the last element of the array;
- $_ will be set in turn to the numbers from 0 to 9
- The Perl delete function acts on the odd indices of the array elements only
- the second foreach loop is used to print the elements of the array – to check up if the current element of the array has a value different from undef, the exists function is used
- the Data::Dumper module is used to print the array, too; you can notice that after using the Perl delete function, the array continues to have 10 elements - only the values of the elements were made undef
There is a subtle difference when you use undef or delete against an array element. In both cases the size of the array remains unchanged and the value of the element is assigned to undef.But if you check the existence of the element with the exists function, this function returns true if you initialized the array element with undef and false if you deleted the element with delete.
See the following code snippet:
my @array = 0..10;
$array[0] = undef;
printf "Using undef: Still there\n" if exists $array[0];
# it prints Using undef: Still there
delete $array[1];
printf "Using delete: It's not there\n" unless exists $array[1];
# it prints Using delete: It's not there
See below a few examples about how you can use the Perl delete function with a hash:
my %phoneNumbers = ("Anne", "408566", "Paul", "233375", "Marie",
"217302");
delete $phoneNumbers {"Paul"};
delete $phoneNumbers {"John"};
# define a reference to a hash
my $phoneNumbersRef = \%phoneNumbers;
delete $$phoneNumbersRef{"Anne"};
# delete $phoneNumbersRef->{"Anne"};
#print the hash (keys, values)
foreach my $key (keys % phoneNumbers) {
print "Key: $key, Value: $phoneNumbers{$key}\n";
}
The first call to the Perl delete function of this code snippet will have as effect the removing of the "Paul" key from the %phoneNumbers hash. In the next line of code we try to delete an inexistent key from the hash, the script will not return any error and the %phoneNumbers hash will remain unchanged. In this case the delete function will return the undef value.
As a note, to delete a key when you have a hash reference you can use either the notation $$hashRef{$key} or $hashRef->{$key}.
The output is as follows:
Key: Marie, Value: 217302
The next example shows you how to use the Perl delete function to remove a slice from a hash:
my %hash = (one => 1, two => 2, three => 3, four => 4);
my @val = delete @hash{qw(one three)};
print "\%hash = (";
print map { "$_ => $hash{$_}, " } keys %hash;
print ")\n\@val = @val\n";
The output produced after running this code is as follows:
%hash = (two => 2, four => 4, )
@val = 1 3
In the example above @hash{qw(one three)} represents a slice of %hash. The Perl delete function having it as argument will return in @val a list with the values corresponding to the pair elements that are removed from %hash.
You can use delete @hash{keys %hash} to empty a hash, but this is slower than any of the following classic ways:
%hash = (); # empty the hash
undef %hash; # undefine the hash
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl built-in functions in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :