Perl: For loop

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


Loops run a particular piece of code as long as a certain condition remains. This is generally called flow control.

Perl has 'for' loop similar to C: for ($i=0;$i<10;$i++){ print "count is $i \n"; }

Another Perl for loop example:

     for $i (1, 2, 3, 4, 5) {
         print "$i\n";
     }

This loop prints the numbers 1 through 5 each on new line as follows:

1
2
3
4
5

The items in your perl for loop can be strings as well. As an example look below:

    for $i (keys %month_has) {
        print "$i has $month_has{$i} days.\n";
    }

    for $var ('a', 'b', 'c', 'd') {
 
        print "$var is the variable.\n";
    }

This would output:

a is the variable
b is the variable
c is the variable
d is the variable

Some of the other details is here: Perl for





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.