Working with repeating spans - an extended example

In the variables within repeat spans section, we learned that when you define a repeat span, the variables inside are repeated. This happens each time you define a repeat span meaning repeat spans can be included inside other repeat spans - making them
nested repeat spans
.
Let us consider a corporation having multiple companies, where each separate company has multiple directors and multiple classes of shares:
The Corporation:
{ CorporationName }
We now have our first repeat span - the companies are repeated 1
To
CompanyCount
:
The Companies:
[
Repeat
CompanyCount
Company #{
RepeatCounter
}
Name: { CompanyName }
Share Classes:{ CompanyShareClasses } ]
At this point, the generated document may look like this:
The Corporation:
ABC
The Companies:
Company #1
Name: Alpha Holdings
Share Classes: Class A, Class B
Company #2
Name: Beta Construction
Share Classes: Class A, Class C, Class D
Now consider each company also having multiple directors. Here comes our second repeat- and we will nest it inside the first:
The Corporation:
{ CorporationName }
The Companies:
[
Repeat
CompanyCount
Company #{
RepeatCounter
Name: { CompanyName }
Share Classes:{ CompanyShareClasses }
The Directors:
[
Repeat
DirectorCount
Director #{
RepeatCounter
}
Name: { DirectorName }
Age:{DirectorAge } ] ]
Now our generated document looks like this:
The Corporation:
ABC
The Companies:
Company #1
Name: Alpha Holdings
Share Classes: Class A, Class B
The Directors
Director #1
Name: Jane Doe
Age: 40
Director #2
Name: John Buck
Age: 50
Director #3
Name: Catherine Ferguson
Age: 30
Company #2
Name: Beta Construction
Share Classes: Class A, Class C, Class D
The Directors
Director #1
Name: Michael McMillan
Age: 60
Finally let us consider each director owns shares in the classes available to that company. This will produce our 3rd nested repeat:
The Corporation:
{ CorporationName }
The Companies:
[
Repeat
CompanyCount
Company #{
RepeatCounter
}
Name: { CompanyName }
Share Classes:{ CompanyShareClasses }
The Directors:
[
Repeat
DirectorCount
Director #{
RepeatCounter
}
Name: { DirectorName }
Age:{DirectorAge }
[
Repeat
UnRepeated
( CompanyShareClasses
)
Shares: { DirectorOwnsShares } of {
RepeatCounter
}
&cd;
]
We can see the effect of the
RepeatCounter
in the full example of the generated document below:
The Corporation:
ABC
The Companies:
Company #1
Name: Alpha Holdings
Share Classes: Class A, Class B
The Directors:
Director #1
Name: Jane Doe
Age: 40
Shares: 0 of Class A, 1000 of Class B
Director #2
Name: John Buck
Age: 50
Shares: 2000 of Class A, 0 of Class B
Director #3
Name: Catherine Ferguson
Age: 30
Shares: 3000 of Class A, 3000 of Class B
Company #2
Name: Beta Construction
Share Classes: Class A, Class C, Class D
The Directors:
Director #1
Name: Michael McMillan
Age: 60
Shares: 1000 of Class A, 2000 of Class C, 3000 of Class D

Advanced collect functions

We have looked previously at using the
Collect
function to build a list of repeated variables (See variables within repeat spans). What if we wanted to refer to variables that are nested deeper than the first level?
For example, at the (non-repeated) corporation level, we can refer to the names of all directors in any of its companies:
The directors in all of { CorporationName }'s companies are {
Collect
( DirectorName )
Format
", | and |" }
which will appear in the generated document as:
The directors in all of ABC's companies are Jane Doe, John Buck, Catherine Ferguson and Michael McMillan
For example, to find the average age of all the directors in the corporation:
The average directors' age in all of { CorporationName }'s companies is {
Mean
(
Collect
( DirectorAge ) ) }
which will appear in the generated document as:
The average directors' age in all of ABC's companies is 45
whereas to find the average age of all the directors in each company:
[
Repeat
CompanyCount
The average directors' age in { CompanyName } is {
Mean
(
Collect
( DirectorAge ) ) } ]
which will appear in the generated document as:
The average directors' age in Alpha Holdings is 40
The average directors' age in Beta Construction is 60
That is, the
Collect
function will automatically find the appropriate repetition level for the variables mentioned in the function, relative to where the function occurs.

Manipulating the collected list of repeated values

The
Collect
function, in its simplest form above, merely collects together
all
the repeated values in the
order
they were originally entered by the questionnaire user. Both of these (
all
and
order
) can be manipulated by adding further arguments to the
Collect
function.
For example, to return those companies which have at least 3 directors:
{
Collect
( CompanyName, DirectorCount
IsAtLeast
3 ) }
which will appear in the generated document as:
Alpha Holdings
For example, to return those directors under 45:
{
Collect
( DirectorName, DirectorAge
IsLessThan
45 ) }
which will appear in the generated document as:
Jane Doe
Catherine Ferguson
For example, to return the full repeat context of those directors under 45:
{
Collect
( RepeatContext, DirectorAge
IsLessThan
45 ) }
which will appear in the generated document as:
[1][1]
[1][3]
For example, to return all directors in ascending order:
{
Collect
( DirectorName,
true
, DirectorName ) }
which will appear in the generated document as:
Catherine Ferguson
Jane Doe
John Buck
Michael McMillan
For example, to return all directors from oldest to youngest:
{
Collect
( DirectorName,
true
,
Inverse
( DirectorAge )) }
which will appear in the generated document as:
Michael McMillan
Catherine Ferguson
Jane Doe
John Buck
For example, to return the number of shares owned, other than none at all, in ascending order:
{
Collect
( DirectorOwnsShares, DirectorOwnsShares
IsNot
0, DirectorOwnsShares ) }
which will appear in the generated document as:
1000
1000
2000
2000
3000
3000
3000

Variables to support the data model

The following table names the variables which support the data model, and indicates how they are repeated.
Variable
Description
Repetition
CorporationName
The name of the corporation
none
CompanyCount
The number of companies in the corporation
none
CompanyName
The name of an individual company in the corporation
1
To
CompanyCount
CompanyShareClasses
The list of share classesoffered byeach company
1
To
CompanyCount
DirectorCount
The number of directors ineach company
1
To
CompanyCount
DirectorName
The name of an individual director in a particular company
1
To
CompanyCount
1
To
DirectorCount
DirectorAge
The age of an individual director in a particular company
1
To
CompanyCount
1
To
DirectorCount
DirectorOwnsShares
The number of shares in a particular share class that are owned by an individual director in a particular company
1
To
CompanyCount
1
To
DirectorCount CompanyShareClasses

Example data

The following tables contain example values for the variables above.
The Corporation
Neither of these variables are repeated.
Variable
Value
CorporationName
ABC
CompanyCount
2
The Companies
These variables are repeated 1
To
CompanyCount
. Thus, repetition [1] indicates the first company and repetition [2] indicates the second company.
Repetition
Variable
Value
[1]
CompanyName
Alpha Holdings
[1]
CompanyShareClasses
Class A
Class B
[1]
DirectorCount
3
[2]
CompanyName
Beta Construction
[2]
CompanyShareClasses
Class A
Class C
Class D
[2]
DirectorCount
1
The Directors
These variables are repeated 1
To
CompanyCount
, and for each company 1
To
DirectorCount
. Thus, repetition [1][1] indicates the first director of the first company, repetition [1][2] indicates the second director of the first company, repetition [1][3] indicates the third director of the first company, and repetition [2][1] indicates the first director of the second company.
Repetition
Variable
Value
[1][1]
DirectorName
Jane Doe
[1][1]
DirectorAge
40
[1][2]
DirectorName
John Buck
[1][2]
DirectorAge
50
[1][3]
DirectorName
Catherine Ferguson
[1][3]
DirectorAge
30
[2][1]
DirectorName
Michael McMillan
[2][1]
DirectorAge
60
The Shares Owned
This variable is repeated 1
To
CompanyCount
, and for each company 1
To
DirectorCount
, and for each director the
CompanyShareClasses
offered by that company. Thus, for example, repetition [1][1][Class A] indicates the class A shares for the first director of the first company, and repetition [2][1][Class C] indicates the class C shares for the first director of the second company.
Repetition
Variable
Value
[1][1][Class A]
DirectorsOwnsShares
0
[1][1][Class B]
DirectorsOwnsShares
1000
[1][2][Class A]
DirectorsOwnsShares
2000
[1][2][Class B]
DirectorsOwnsShares
0
[1][3][Class A]
DirectorsOwnsShares
3000
[1][3][Class B]
DirectorsOwnsShares
3000
[2][1][Class A]
DirectorsOwnsShares
1000
[2][1][Class C]
DirectorsOwnsShares
2000
[2][1][Class D]
DirectorsOwnsShares
3000