www.perl.com
Perl Programming
Loop Statements
  • for
    • Like C: for (initialization; condition; increment)
  • foreach
    • Iterates over a list or array
  • Good to localize loop variables with my

        for (my $i = 10; $i >= 0; $i--) {
            print "$i...\n";                # Countdown
        }

        foreach my $i (reverse 0..10) {
            print "$i...\n";                # Same
        }

        %hash = (dog => "lazy", fox => "quick");
        foreach my $key (keys %hash) {
            print "The $key is $hash{$key}.\n";  # Print out hash pairs
        }
      

        The fox is quick.
        The dog is lazy.
      
Previous http://stuff.mit.edu/iap/perl/