Collections and foreach

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Code Generator > SPL (Spy Programming Language) >

Collections and foreach

Collections and iterators

A collection contains multiple objects - like a ordinary array. Iterators solve the problem of storing and incrementing array indexes when accessing objects.

 

Syntax:

 

foreach iterator in collection

 statements

next

 

Example:

 

[foreach $class in $classes

 if not $class.IsInternal

         ]        class [=$class.Name];

[        endif

next]

 

Example 2:

 

[foreach $i in 1 To 3

      Write "// Step " & $i & "\n"

      ‘ Do some work

next]

 

 

Foreach steps through all the items in $classes, and executes the code following the instruction, up to the next statement, for each of them.

 

In each iteration, $class is assigned to the next class object. You simply work with the class object instead of using, classes[i]->Name(), as you would in C++.

 

All collection iterators have the following additional properties:

 

Index

The current index, starting with 0

IsFirst

true if the current object is the first of the collection (index is 0)

IsLast

true if the current object is the last of the collection



 

Example:

 

[foreach $enum in $facet.Enumeration

 if not $enum.IsFirst

         ], [

 endif

 ]"[=$enum.Value]"[

next]

 

 

Collection manipulation routines:

 

collection SortByName( bAscending )

returns a collection whose elements are sorted by name (case sensitive) in ascending or descending order.

 

collection SortByNameNoCase( bAscending )

returns a collection whose elements are sorted by name (case insensitive) in ascending or descending order

 

Example:

$SortedNestedClassifier = $Class.nestedClassifier.SortByNameNoCase( true )

 

collection SortByKind( bAscending )

returns a collection whose elements are sorted by kind names (e.g. “Class”, “Interface”,…) in ascending or descending order.

 

collection SortByKindAndName( bAscendingKind, bAscendingName )

returns a collection whose elements are sorted by kind (e.g. “Class”, “Interface”,…)  in ascending or descending order and if the kinds are equal by name (case sensitive in ascending or descending order)

 

collection SortByKindAndNameNoCase( bAscending )

returns a collection whose elements are sorted by kind (e.g. “Class”, “Interface”,…) in ascending or descending order and if the kinds are equal by name (case insensitive in ascending or descending order)


© 2019 Altova GmbH