Perl Hash Element each() Function

What is each() Function in Perl programming?


The each() function is used to retrieve the next key/ value pair from a hash if its used in a list context , if called in a scalar context only the next key of the hash is retreived.


each hash;

Example :

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
%color = ('1' => 'Green' , '2' => 'Orange',
'3' => 'Blue', '4' => 'Pink');
while (($key, $value) = each %color)
print "$key=$valuen";
Result :

4=Pink 1=Green 3=Blue 2=Orange

In the above example using the each() function every time in a loop a key, value pair is fetched and displayed. As you can see that the pairs returned are random and not in the same order as they are in a hash.

Ask Questions

Ask Question