Example – Different ways for handling arrays/tables in SE

1 82 Views No Comments 2 months ago
  • Different ways for handling arrays/tables in SE

    • This topic was modified 1 month, 3 weeks ago by pshai. Reason: added 5th example
    • This topic was modified 1 month, 3 weeks ago by pshai.
    HaasScript Code
    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
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free