Log In  

I know this is kind of lazy, and you could just loop over a table with "pairs" and collect all of the keys, but is there a built in function that will return a table of any given table's keys? If not, then I'll implement my own, but I don't want to waste tokens where unnecessary.

Cheers in advance!

P#90319 2021-04-10 11:38

I can see how that'd be useful, but it's niche enough that it hasn't (to my knowledge) been implemented in PICO-8's "cozy" little API.

(I think even in regular Lua there's no helper for that, but I could be wrong.)

I'd probably just do it manually, as you indicated yourself:

for k in pairs(a) do b[#b+1]=k end
P#90321 2021-04-10 12:27
1

you could even implement it as an iterator so that you can use it like all():

function keys(t)
    local ks={}
    local i=0
    for k in pairs(t) do
        add(ks,k)
    end

    return function()
        i+=1
        if(i<=#ks)return ks[i]
    end
end

Then you can do for k in keys(obj) do...

P#90399 2021-04-12 14:04

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 17:04:28 | 0.007s | Q:13