next up previous contents
Next: Functions Up: If/Unless statement Previous: The while/until statement

The foreach statement

The foreach statement iterates through items in a list: The statement has the following format:

foreach $i (@some_list)
  {  # $i takes on each list item value in turn
     statement(s);
  }

For example (foreach.pl):

@a = (1, 2, 3, 4, 5);
foreach $i (reverse @a)
  { # reverse is a functiion that flips the list order
    print $i;
  }



dave@cs.cf.ac.uk