Example six - FOR and FOREACH loop structures.


# Example six - FOR and FOREACH loop structures.
# The same loop shown in example 5a is now controlled by FOR
# Also a basic illustration of FOREACH is shown.
# by Al Bento

#variables used in the for example

$lower = 0; # lowest temperature
$upper = 300; # highest temperature
$step = 20; # increment step

# a simple for structure

for ($fahr = $lower; $fahr <= $upper; $fahr = $fahr + $step) {
     $celsius = 5/9 * ($fahr-32);
     print "$fahr $celsius\n";
}

#variables used in the foreach example

@users = ("pete","root","al");

#printing the values

     print "\n\tThe users are $users[0], $users[1], and $users[2]\n";

# a simple foreach example

foreach $user (@users) {

     print "\t$user is a Linux user\n";
}

download the script


This page is maintained by Al Bento who can be reached at abento@ubmail.ubalt.edu. This page was last updated on February 15, 2001. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.