Template Engine language reference > Arrays > Array examples

Array examples
Example 9
Declaring an array using fixed values for the elements.
Template
#set($array=[1,'house', 99.33])
$array
$array[0]
$array[1]
$array[2]
Output
[1, house, 99.33]
1
house
99.33
 
Example 10
Declaring an array using variable values for the elements. The output is the same as in the previous example.
Template
#set($int=1) #set($string='house') #set($dec=99.33)
#set($array=[$int, $string, $dec])
$array
$array[0]
$array[1]
$array[2]
Output
[1, house, 99.33]
1
house
99.33
 
Example 11
Declaring an array using arrays as element values.
Template
#set($array=[['A', 'B'], ['C', 'D'])
$array[0][0]
$array[0][1]
$array[1][0]
Output
A
B
C
 
Example 12
Declaring an array using array variables as element values.
Template
#set($array1=['A', 'B']) #set($array2=['C', 'D'])
#set($array=[$array1, $array2])
$array[0][0]
$array[0][1]
$array[1][0]
Output
A
B
C
 
Example 13
Declaring an array using array variables as element values. The first array variable ($array1) is used in both $array2 and $array.
Template
#set($array1=['A', 'B']) #set($array2=[$array1, 'D'])
#set($array=[$array1, $array2])
$array[0][0]
$array[0][1]
$array[1][0][1]
Output
A
B
B
 
Example 14
Declaring arrays using ranges of integers.
Template
#set($array1=[1..10]) #set($array2=[19..12]) #set($array3=[-3..5])
$array1
$array2
$array3
$array1[3]
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[19, 18, 17, 16, 15, 14, 13, 12]
[-3, -2, -1, 0, 1, 2, 3, 4, 5]
4
 
Example 15
Using the #foreach directive to loop over an array.
Template
#set($array=[1..5])
#foreach($i in $array) $i #end
Output
1
2
3
4
5
 
Example 16
Using the #foreach directive to loop over a range of integers and dynamically add elements to an array.
Template
#set($array=[])
#foreach($i in [1..5]) $array.add($i) #end
$array
Output
[1, 2, 3, 4, 5]
 
Example 17
Using the .size() function to count and return the number of elements in an array.
Template
#set($array=[2,40,367])
$array.size()
Output
3
 
Example 18
Using the .isEmpty() function to check whether arrays are empty.
Template
#set($array1=[2,40,367])
#set($array2=[])
$array1.isEmpty()
$array2.isEmpty()
Output
false
true
 
Example 19
Using the .clear() function to clear an array.
Template
#set($array=[2,40,367])
$array
$array.clear()
$array
Output
[2,40,367]
[]
 
Example 20
Using the .add() function to append an element at the end of an array.
Template
#set($array=[0,1,2])
$array
$array.add(3)
$array
Output
[0,1,2]
[0,1,2,3]
 
Example 21
Using the .add() function to add an element to a specific position in an array. The syntax is .add(<index>,<value>).
Template
#set($array=[2,4,8])
$array
$array.add(2,6)
$array
Output
[2,4,8]
[2,4,6,8]
 
Example 22
Using the .remove() function to remove an element from an array. The syntax is .remove(<index>).
Template
#set($array=[2,4,6])
$array
$array.remove(1)
$array
Output
[2,4,6]
[2,6]
 
Example 23
Using the type() function to check the types of the array elements.
Template
#set($var=20)
#set($array=[10, 'a', 99.99, [1,2], $var])
$array
$array[0].type()
$array[1].type()
$array[2].type()
$array[3].type()
$array[4].type()
Output
[10, a, 99.99, [1, 2], 20]
int
string
double
array
int
 
Example 24
Using the toInt() function to convert an array of double values to an array of int values. The #foreach directive is used to loop over the array, the .remove() function is used to remove the original double value and the .add() function is used to add the new int value.
Template
#set($array=[10.6, 0.2, 99.99, 11.32, 20.55])
Original array: $array
#set($size=$array.size())
#foreach($i in [1..$size])
  #set($index=$i-1)
  #set($int=$array[$index].toInt())
  $array.remove($index)
  $array.add($index,$int) 
#end
Modified array: $array
Output
Original array: [10.6, 0.2, 99.99, 11.32, 20.55]
Modified array: [11, 0, 100, 11, 21]
 
Example 25
Using the toInt() function to convert double to int in an array that contains values of both types. The #foreach directive is used to loop over the array, the #if directive and type() function are used to check whether the type is not int, the .remove() function is used to remove the original double value and the .add() function is used to add the new int value.
Template
#set($array=[10.6, 2, 99, 11.32, 20.55])
Original array: $array
#set($size=$array.size())
#foreach($i in [1..$size])
  #set($index=$i-1)
  #set($type=$array[$index].type())
  #if($type != 'int')
    #set($int=$array[$index].toInt())
    $array.remove($index)
    $array.add($index,$int)
  #end 
#end
Modified array: $array
Output
Original array: [10.6, 2, 99, 11.32, 20.55]
Modified array: [11, 2, 99, 11, 21]
 
Example 26
Using the toString() function to convert int values to string values.
Template
#set($array=[100,200,300])
Integer addition: 
#evaluate($array[0]+$array[2])
 
String addition: 
#evaluate($array[0].toString()+$array[2].toString())
Output
Integer addition: 
400
String addition: 
100300
 
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26