<for>

<for> creates a set of data transformation operations based on the variables and specifications of the construct.

Description

During block code expansion, the contents of the <for> construct are inserted as data transformation operations, with variables substituted accordingly, on each iteration of the loop as determined by specifications of the construct. These specifications include the initial and terminating values of the loop variable as well as an optional increment value and tally variable.

Syntax

<for [VAR]="[STARTING_VALUE]" to_="[TERMINATING_VALUE]" 
     by_="[INCREMENT_INTERVAL]" tally_="[TALLY_VAR]">
     [CONTENTS]
</for>

Attributes

[VAR]
[VAR] is an arbitrary variable name that can be used within the contents of the <for> loop on each iteration. The value assigned to this variable should be a numerical value that specifies its initial value for the first iteration of the loop.
to_
Accepts a numerical value that specifies the terminating value of the iterated variable. Once [VAR] is greater than this value, the <for> loop ends.
by_
Accepts a numerical value that specifies the amount to increment the value of [VAR] on each iteration of the <for> loop.

The default is 1.

tally_
Accepts the name of a variable whose value corresponds to the number of the iteration. The value of the variable can be used within the contents of the <for> loop on each iteration.

The value of the variable is initially set to 1 and is incremented by 1 on each iteration.

Example

In this example, the <for> block construct is used to create a series of <sel> operations based on the values of two variables that contain delimited strings. The to_ attribute sets the terminating value to 3 by using the strcount(X;Y) function to determine the number of values in the semicolon-separated string in the colnames variable. On each iteration of the <for> loop, a <sel> operation is constructed using the values in the colnames and values variables based on the value of the variable i.

<base table="pub.demo.retail.item"/>
<block colnames="store;date;units" values="1-20120603-1">
   <for i="1" to_="{strcount(@colnames;';')+1}">
      <sel value="{strpick(@colnames;';';@i)}='{strpick(@values;'-';@i)}'"/>
   </for>
</block>

On the first iteration, when i=1, the strpick(X;Y;I) function uses the first entry in colnames and values to build the first <sel> statement, which results in:

<sel value="store='1'"/>

The second time through, i is set to 2, so strpick(X;Y;I) uses the second entry in colnames and values; and the third time it uses the third entry.

After block code expansion, this results in the following three <sel> statements:

<sel value="store='1'"/>
<sel value="date='20120603'"/>
<sel value="units='1'"/>

Example: Using by_ and tally_

The following example uses a <for> loop to create a set of computed columns. It uses the by_ attribute to specify that the variable i should be incremented by .5 on each iteration. It also specifies a variable named j for the tally_ attribute, which is set to the number of the iteration; the value of that variable is used in the name of each computed column. It then uses <colord> to display only those columns that begin with col, which in this case are the columns created by the <for> loop.

<base table="default.lonely"/>
<for i="2.5" to_="5.5" by_=".5" tally_="j">
  <willbe name="col{@j}" value="{@i}"/>
</for>
<colord cols="col*"/>