Perl | Useful Hash functions

A hash is a set of key-value pairs. Perl stores elements of a hash such that it searches for the values based on its keys. Perl provides various functions to perform operations on Hashes, such as to return values of the hash, to delete elements from a hash, etc.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl 
    
# Initializing hash  
%hash1 = ('Welcome' => 10, 'to' => 20, 'Geeks' => 80);  
    
# To delete the List passed as parameter 
$deleted_element = delete($hash1{'to'}); 
    
# Printing elements of Hash  
print "$hash1{'Welcome'}\n";  
print "$hash1{'to'}\n";  
print "$hash1{'Geeks'}\n";  
    
# Printing the deleted element 
print "Deleted element: $deleted_element"

chevron_right


Output:

10

80
Deleted element: 20

Some useful functions for hash operations in Perl are listed below:



Function Description
values() Returns the list of all the values stored in a Hash
keys() Returns all the keys of the HASH as a list
each() Returns a Two-element list consisting of the key and value pair in List context and key for the next element when called in scalar context
delete() Used to delete the specified keys and their associated values from a hash, or the specified elements in the case of an array


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.