next up previous contents index
Next: While Up: Control Structures Previous: If   Contents   Index


The control structure `foreach' works its way through an array one item at a time.

    foreach $item (1, 2, 3 ,4 ,5 ) {
        print "Item: $item\n";

In this example the array is the numbers from 1 to 5, each is put into the variable `$item' and then printed.

If the item isn't explicitly mentioned, it is stored in the perl default variable $_.

    @alphabet = (a..z);
    foreach (@alphabet) {
        print "$_,";
    print "\n";

Tom Hunt 2002-06-09