TYL PROGRAMMING LANGUAGE
▶ RECORD ITERATOR
Tyl Record Iterator is similar to list iterator that was described in the last chapter. It is a looping mechanism for iterating over record items and do operations for each item.

Record iterator signature:
[RECORD] [ITEM KEY] [ITEM VALUE] ~

Iteration over a record is enabled only if all items are of the same type.

Iterating over a record with scalar values:
data { k1 'v1' k2 10 }
data key val ~ print key + ' = ' + val
k1 = v1
k2 = 10
Statement 'data key val', is the record iterator selector, and for each iteration, key and val will be declared as local variables of the looping, that hold the current item's key and value respectively. These variables will mask all outside variables except function parameters.

Changing a record inside the record iterator is enabled. Another way to do it is by iterating over the record keys:
data { k1 'v1' k2 10 }
keys
data key val ~ keys <- key
keys key ~
    val data key
    not null number.of val ? data key 0
^
print data
{
 k1: 'v1'
 k2: 0
}
After filling keys list with data keys, statement not null number.of val is checking if val is of numeric type, whereupon it changes the current data key value to 0.

Note that if we set val to 0, it will not affect data key value, because val holds a copy of data key. Likely, setting a value to a record iterator selector variables will not affect the record:
data { k1 'v1' k2 10 }
data key val ~ print key + ' = ' + val 0
print data
k1 = 0
k2 = 0
{
 k1: 'v1'
 k2: 10
}
Inside the looping repeat block, val was set to 0, as seen in the results, but data record was not affected.

Iterating over a record with list values:
data { }
numerator 1
i 2 ~
    itemdata [ ]
    j 3 ~ itemdata <- number.of string.section random 0 6
    data <- numerator itemdata
    numerator ++
^
print data
print
data key vals ~ print key + ': ' + vals
{
 1: [ 0.6572, 0.5254, 0.401 ]
 2: [ 0.2506, 0.1365, 0.9858 ]
}

1: [ 0.6572, 0.5254, 0.401 ]
2: [ 0.2506, 0.1365, 0.9858 ]
In line data <- numerator itemdata, a repeatedly increasing numerator number is added to data as key, with a list of random numbers as value. The looping selector data key vals, has vals variable for the current record list value. As in function list parameters, the name of a list variable in the selector statement must be in plural ending format.

Record iterator list value variable can be used to iterate over its items inside the looping block:
data { }
data <- as [ 11 12 ]
data <- bs [ 21 22 ]
data key vals ~ vals val ~ print string.replace ( key + ' ' + val ) 's' ''
a 11
a 12
b 21
b 22
Iterartor 'vals val ~', was chained to record iterator 'data key vals ~', to form a chain of iterator statements, where val have the value of current record item value's item.
PART 3
PART 5