Tuesday, 15 April 2014

Lua table initialization - what is incorrect here -



Lua table initialization - what is incorrect here -

this question has reply here:

lua: iterate through pairs in table 1 reply

i trying initialize , print table. isnt working. thought wrong code?

--!/usr/bin/env lua local retv = {} retv["test"] = 1000 k,v in ipairs(retv) print (k,v) end

it prints nothing. sure missing basic cant figure out.

there 2 forms of for-loop in lua: numeric , generic for-loop.

ipairs(t) iterator constructor returning 3 arguments suitable generic for, allowing iterate on initial sequence (indices 1,2,3,...) in order. possible implementations:

function ipairs(t) local = 0 homecoming function() = + 1 if t[i] ~= nil homecoming i, t[i] end end end local function ipairs_helper(t, i) = + 1 if t[i] ~= nil homecoming i, t[i] end end function ipairs(t) homecoming ipairs_helper, t, 0 end

as can see, never homecoming entry key "test".

what want instead, pairs(t), equivalent next, t. iterate elements.

lua initialization lua-table

No comments:

Post a Comment