|
|
||||
![]() | ||||
These two commands repeat a segment of code any number of times. Which one you use depends on the way you are using the data within the loop.
.FOR VarName= [Start=] End= [Step=]$, because this is just the variable's name.
1 is
assumed.
.FOR
command, where Start and End refer to the starting and ending indices in the
array.
.FOR or .FOREACH loop.* The variable whose name is VarName is always set locally to the loop. This means that any previous value of this variable is automatically restored when the loop is done. For more information, see this section on local variables.
*
If the variable being iterated over in a .FOREACH is a disk-based
database file, the variable db_key is set to the key (or index)
value of each record, as the loop executes. This variable is local to the loop
(restored automatically when the loop is done), so loops can be nested and their
db_key will behave as expected.
There are two ways that a function can be invoked. The first, is by simply
referencing it using the & symbol, followed by its name. When
using this syntax, you can separate any parameters being passed to the function
with spaces or commas:
<!--. echo "The numbers 1 to 25:"; for n 1 25; echo " $n"; endfor; echo "<P>Odd numbers from 1 to 25:"; for n 1 25 2; echo " $n"; endfor; echo "<P>Counting down from 10 to 0:"; for n 10 0 -1; echo " $n"; endfor; set arr[0]='zero'; set arr[1]='one'; set arr[2]='two'; echo "<P>First method, arr contains:"; for n 0 "$&countcols('$arr')"-1; echo " $arr[$n]"; endfor; echo "<P>Second method, arr contains:"; foreach a "$arr"; echo " $a"; endfor; echo "<P>This time, just show the first two elements:"; foreach a "$arr" 0 1; echo " $a"; endfor; -->The numbers 1 to 25: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Odd numbers from 1 to 25: 1 3 5 7 9 11 13 15 17 19 21 23 25
Counting down from 10 to 0: 10 9 8 7 6 5 4 3 2 1 0
First method, arr contains: zero one two
Second method, arr contains: zero one two
This time, just show the first two elements: zero one
Home | Directories | Web Email | Calendar | Campus Map | Search | Help
About the College |
Admission |
Academics |
Student Life |
Athletics Copyright © 2002 Mount Holyoke College. This page created by the OIS Operations Group and maintained by Webmaster. Last modified on June 10, 2002. |