This function returns a Two-element list consisting of the key and value pair for the next element of a hash when called in List context, so that you can iterate over it. Whereas it returns only the key for the next element of the hash when called in scalar context.
Syntax: each MY_HASH
Parameter:
MY_HASH is passed as a parameter to this functionReturns:
A 2-element list of key-value pairs for the List context whereas only the key for the scalar context.
Example 1:
#!/usr/bin/perl # Initializing a Hash %hash = (Geeks => 1, of => 2 , Geek => 3); # each() function while (($key, $value) = each(%hash)) { # Printing(key, value) pair print("$key = $value\n"); } |
Geek = 3 of = 2 Geeks = 1
Example 2:
#!/usr/bin/perl # Initializing a Hash %hash = (Geeks, of, Geek); # each() function for scalar context while (($key) = each(%hash)) { # Printing(key) print("$key "); } |
Geek Geeks
Recommended Posts:
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | split() Function
- Perl | chomp() Function
- Perl | chop() Function
- Perl | rename() Function
- Perl | undef and the defined function
- Perl | cos() Function
- Perl | sin() Function
- Perl | abs() function
- Perl | atan2() Function
- Perl | splice() - The Versatile Function
- Perl | delete() Function
- Perl | defined() Function
- Perl | index() Function
- Perl | int() function
- Perl | join() Function
- Perl | keys() Function
- Perl | lc() Function for Lower Case Conversion
- Perl | lcfirst() Function
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.

