Help Search Campus Map Directories Webmail Home Alumnae Academics Admission Athletics Student Life Offices & Services Library & Technology News & Events About the College Navigation Bar
MHC Home Using the Document Interface
TopNext Page

.FOR and .FOREACH

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=]
The VarName parameter is required. It defines the name of a variable to hold the data that gets changed each time through the loop. Be careful not to start it with $, because this is just the variable's name.

Start is the integer value to begin with. If omitted, zero is assumed.

End is the ending value (inclusive) for the variable. If omitted, the code within the loop will not execute.

Step is added to the value each time through the loop, until End (or further) is reached. If Step is omitted, a value of 1 is assumed.

.FOREACH VarName= [DBVar=] [Start=] [End=] [Step=]
This command is used to loop repeatedly based on the contents of an array. The majority of the parameters to this command are identical to the .FOR command, where Start and End refer to the starting and ending indices in the array.

If the array passed in DBVar is one-dimensional, then the loop repeats for each column. If it's two-dimensional, the loop repeats once for each row, and the variable gets set to a one-dimensional array for that row.

It is a good idea to always put the DBVar argument in "quotes", so that the DI will not try to parse it as though it has embedded spaces.

.ENDFOR
Signifies the end of a .FOR or .FOREACH loop.

Notes about loops

* 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.


Examples

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

----------------------------------------
Top | Next Page

Home | Directories | Web Email | Calendar | Campus Map | Search | Help

About the College | Admission | Academics | Student Life | Athletics
Offices & Services | Giving | News & Events | Alumnae | Library & Technology

Copyright © 2002 Mount Holyoke College. This page created by the OIS Operations Group and maintained by Webmaster. Last modified on June 10, 2002.