The following infopad is used for demonstrating iterations. It has cells containing two attributes: one of the integer type and one of the string type. mypad is a fixed-size infopad.
initialize {
mypad := new
infopad<cell{attnum:int,attstr:string}>[10][5]("mypadname");
}
Note: You must remember that infopad index values start from 1. For example, when an infopad is declared of dimensions [10][5], this means its row index takes values from 1 to 10, and its column index values go from 1 to 5.
Initializing to iterate on infopad cells
This section describes how a rule—or an initial statement in the initialize section of a rule module—can iterate on the cells of an infopad.
If all the initial values are known for all the slots of a cell, then we can use the following single infopad method call: setAll(). All the slots of a cell are specified here (except the default count slot, which is always initialized to 0, unless specified otherwise).
mypad.setAll(cell{attnum:0, attstr:"nothing"});
Initialization is also done with a loop statement. This allows more control on which slots are initialized, and in which cells. In the statement below for the fixed-sized infopad, the identifiers rt and ct represent a constant (or value) that is not changeable (only initialized), as it is declared with the keyword val. These constant values are initialized to the current row count and column count of the infopad, using the functions rowCount() and colCount(). The two nested for loops explicitly iterates over the row and column indexes.
val rt= mypad.rowCount();
val ct= mypad.colCount();
for (var i = 1; i <= rt; i++ )
for (var j= 1; j<= ct; j++ )
{ mypad[i][j].attnum := 0; mypad[i][j].attstr := "nothing"; }
General iterations with tests
You may introduce additional selection conditions in loop statements, as in regular programming languages. A similar loop for the cells of the two-dimension mypad infopad are as follows. Note that nested loops are not necessary:
do mypad[xi][yi].attstr := evt1.myname
from xi:mypad.rowKeySet(), yi:mypad.colKeySet()
where (mypad[xi][yi].attnum > 100);
These iteration statements require the creation in memory of value sets resulting from the function calls rowKeySet() and colKeySet(). This may be inconvenient in terms of overhead (memory) for large infopads.
Using scaled infopads
Assume that each row of an infopad named salarystats is associated with a numeric range representing salary range (in $1000): starting from [0-30[, [30-40[, [40-50], etcetera until [120-plus]. Each time a new employee is hired, an event is generated that contains his/her salary. We want to record how many employees are in each salary range. We can find the row (and cell) that corresponds to a particular salary, that is, 43, by iterating over the infopad:
var uppb = 30;
for (var xi = 1; xi <= 10; xi++)
{ if ( evt1.salary < uppb )
{ salarystats[xi][1].count++; break; }
uppb += 10;
}if ( evt1.salary >=120 ) salarystats[11][1].count++;