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 FOR EACH Loop Function


Tutorials Perl

Topic

What is For Each loop function in Perl programming?



Explanation

The For Each loop statement in Perl is used with array to manipulate array elements to get the desired result.

Syntax:
 foreach variable ( array )
  {
   Statement to be executed
  }

In the above syntax for each occurence of the "variable" the "array" is checked, if "true" the code segement given below is executed.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $search = "Kate";
    @actress = ("Angelina", "Pamela", "Katrina", "Jennifer", "Kate");
    foreach $name (@actress) 
     {
      if ($name eq $search)
       {
         $prompt = "$name listed!\n";
         print $prompt;
         print "
"; } else { $prompt = "$name not listed!\n"; print $prompt; print "
"; } }
Result:
    Angelina not listed!
    Pamela not listed!
    Katrina not listed!
    Jennifer not listed!
    Kate listed! 

In the above example the variable "$name" is checked if its equal to the variable "$search" with the elements of the array "@actress", it displays the listed and not listed actress names.






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