Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





Perl Hash Element each() Function


Tutorials Perl

Topic

What is each() Function in Perl programming?



Explanation

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.

Syntax:
    each hash; 
Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    %color = ('1' => 'Green' , '2' => 'Orange', 
       '3' => 'Blue', '4' => 'Pink');
    while (($key, $value) = each %color)
    {
     print "$key=$value\n";
    }
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.






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting