Example - Different ways for handling arrays/tables in SE
stableDescription
Different ways for handling arrays/tables in SE
HaasScript
if Load('init', true) then
-- 1
local p = {
{'test', 512}
}
Log(p[1][1] .. ' = ' .. p[1][2])
Save('test1', p)
-- 2
local p = {
test = 512
}
local k = 'test'
Log(k .. ' = ' .. p[k])
Log(k .. ' = ' .. p.test)
Save('test2', p)
-- 3
local p = {}
p.test = 512
Log('test = ' .. p.test)
Log('test = ' .. p['test'])
Save('test3', p)
-- 4
local p = {}
p['test'] = 512
Log('test = ' .. p.test)
Log('test = ' .. p['test'])
Save('test4', p)
-- 5
local arr = {
module = {
field = 512,
method = function(x, y)
return x + y
end
}
}
Log(arr['module'].field)
Log(arr.module['method'](1, 10))
-- Save command doesnt like the function method we created there,
-- so saving the 5th test object will not work!
Save('init', false)
else
-- load tests
-- 1
local p = Load('test1')
Log(p[1][1] .. ' = ' .. p[1][2])
-- 2
local k = 'test'
local p = Load('test2')
Log(k .. ' = ' .. p[k])
Log(k .. ' = ' .. p.test)
-- 3
local p = Load('test3')
Log('test = ' .. p.test)
Log('test = ' .. p['test'])
-- 4
local p = Load('test4')
Log('test = ' .. p.test)
Log('test = ' .. p['test'])
-- 5
-- we did not save the 5th test, because of the function method...
end
3 Comments
Sign in to leave a comment.
I like the examples like this, but some documentation on exactly what is happening or when one way is recommended over another would be great.
+++ Indeed! Some commented descriptions as a/our guide will greatly compliment your example [+your time inverted, will return you 3x the mana spent ;] here.
Would be great to have this knowledge in a more beginner friendly accessible format for educational purposes.. @pshai
I think this is due for some updates indeed. I will see what I can do!