Perl | each() Function
Last Updated :
25 Jun, 2019
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 function
Returns:
A 2-element list of key-value pairs for the List context whereas only the key for the scalar context.
Example 1:
%hash = ( Geeks => 1, of => 2 , Geek => 3);
while (( $key , $value ) = each ( %hash ))
{
print ( "$key = $value\n" );
}
|
Output:
Geek = 3
of = 2
Geeks = 1
Example 2:
%hash = (Geeks, of, Geek);
while (( $key ) = each ( %hash ))
{
print ( "$key " );
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...