Perl | delete() Function
Last Updated :
25 Jun, 2019
Delete() in Perl is used to delete the specified keys and their associated values from a hash, or the specified elements in the case of an array. This operation works only on individual elements or slices.
Syntax: delete(LIST)
Parameters:
LIST which is to be deleted
Returns:
undef if the key does not exist otherwise it returns the value associated with the deleted key
Example 1: Implementing delete() on a Hash
%hash1 = ( 'Geeks' => 45, 'for' => 30, 'Now' => 40);
$deleted_element = delete ( $hash1 { 'for' });
print "$hash1{'Geeks'}\n" ;
print "$hash1{'for'}\n" ;
print "$hash1{'Now'}\n" ;
print "Deleted element: $deleted_element" ;
|
Output:
45
40
Deleted element: 30
Example 2: Implementing delete() on an Array
@array1 = (10, 20, 30, 40, 50, 60);
$deleted_element = delete ( @array1 [2]);
print "Updated Array: @array1" ;
print "\nDeleted element: $deleted_element" ;
|
Output:
Updated Array: 10 20 40 50 60
Deleted element: 30
Share your thoughts in the comments
Please Login to comment...