# Can't Get My Finger Behind This Puzzle....

behold the source…

``````
fill_table = {}

string.split = function(str, pattern)
pattern = pattern or "[^%s]+"

if pattern:len() == 0 then pattern = "[^%s]+" end

local parts = {__index = table.insert}
setmetatable(parts, parts)
str:gsub(pattern, parts)
setmetatable(parts, nil)
parts.__index = nil
return parts
end

local first_string = "0,12,7,4,12,7,4,3,4,7"
local first_string = "0,4,8,12,16,18,24,32,34,36,40,42,44,48,50,52,54"
local second_string = "0,4,8,12,16,18,24,32,34,36,40,42,44,48,50,52"
local first_value_table = first_string:split( "[^,%s]+" )
local second_value_table = second_string:split( "[^,%s]+" )
local end_figure = #first_value_table
local sub_position = 1
local offset = 0
for t = 0, 80 do
fill_table[t] = 999
end
if #first_value_table < #second_value_table then
end_figure = #second_value_table
end
for t = 0, end_figure-1 do
fill_table[second_value_table[sub_position]+offset] = first_value_table[t+1]
print('position ',second_value_table[sub_position]+offset,
' contains value:',fill_table[second_value_table[sub_position]+offset])
sub_position = sub_position + 1
if sub_position > #second_value_table then
offset = offset + tonumber(second_value_table[sub_position-1])
if tonumber(second_value_table[1]) > 0 then
sub_position = 1
else
sub_position = 2
end
end
else
fill_table[second_value_table[t+1]] = first_value_table[sub_position]
-- print(second_value_table[t+1],fill_table[second_value_table[t+1]])
sub_position = sub_position + 1
if sub_position > #first_value_table then
sub_position = 1
end
print('position ',second_value_table[t+1],' contains value:',fill_table[second_value_table[t+1]])
end
end
print('bound:',#fill_table)

print('value on position 0:',fill_table[0])
print('value on position 4:',fill_table[4])
print('value on position 8:',fill_table[8])

``````

there are two definitions of first_string.
This is the result after execution with the larger of the two defined:

``````
position 0 contains value: 0
position 4 contains value: 4
position 8 contains value: 8
position 12 contains value: 12
position 16 contains value: 16
position 18 contains value: 18
position 24 contains value: 24
position 32 contains value: 32
position 34 contains value: 34
position 36 contains value: 36
position 40 contains value: 40
position 42 contains value: 42
position 44 contains value: 44
position 48 contains value: 48
position 50 contains value: 50
position 52 contains value: 52
position 56 contains value: 54
bound: 80
value on position 0: 0
value on position 4: 4
value on position 8: 8
value on position 1: 999
value on position 5: 999
value on position 9: 999

``````

This is the result with the secondary definition commented out:

``````
position 0 contains value: 0
position 4 contains value: 12
position 8 contains value: 7
position 12 contains value: 4
position 16 contains value: 12
position 18 contains value: 7
position 24 contains value: 4
position 32 contains value: 3
position 34 contains value: 4
position 36 contains value: 7
position 40 contains value: 0
position 42 contains value: 12
position 44 contains value: 7
position 48 contains value: 4
position 50 contains value: 12
position 52 contains value: 7
bound: 80
value on position 0: 999
value on position 4: 999
value on position 8: 999
value on position 1: 999
value on position 5: 999
value on position 9: 999

``````

fill_table (being defined globally) is somehow being restored to its former address after it has been populated when the if condition was not met. But you can see clearly that the values get populated inside the for…do construction, yet as soon as i want to fetch these values outside this function, they are all gone.
The table is not redefined locally anywhere so that is the reason why i don’t understand it keeps the values outside the for-loop if the condition is met but not when the condition is not met.

I’m quite baffled here (But i hope i’m also quite blind or too tired to see something)

I figured it out…
Lua can get sometimes pretty complicated when it is making its own decisions…
The trapdoor lies in a tiny aspect that makes Lua behave in two different ways:

This works:
fill_table[second_value_table[sub_position]+offset] = first_value_table[t+1]

This doesn’t
fill_table[second_value_table[t+1]] = first_value_table[sub_position]

The most important aspect is that second_value_table, is a string defined table.
But when you do a calculation with a number or number defined variable on a string, Lua automatically converts the output to a number.
A pitfall which is not easy to detect if you get good results in one of the cases and not in the other.

The correct solution:
fill_table[tonumber(second_value_table[sub_position])+offset] = first_value_table[t+1]
fill_table[tonumber(second_value_table[t+1])] = first_value_table[sub_position]