Since variables cannot be modified, counters etc. are implemented by IDataHolder arrays where the counter gets the value from an addition of a value to the previous value which is then stored in the current position before advancing to the next position. This mechanism partly breaks in the following scan script, where reading a variable appears to change its value, and I would like to understand why:
# Sum Test
# Build sum starting at the left end
def sum;
if (BarNumber() < 5) {
if (BarNumber() == 1) {
sum = 1;
} else {
sum = sum[1] + 1;
}
} else {
sum = sum[1]; # This causes the problem.
#sum = Double.NaN;# alternative: does not change previous value but useless.
}
# Test that the first sum entry is 1 as expected
plot scan = GetValue(sum, BarNumber() -1) == 1;