Calculator Community > Lua

Lua Q&A

<< < (2/42) > >>

Jim Bauwens:
Well, technically speaking its tables in Lua :)

Lets say you have this table:

--- Code: (Lua) ---a={1,2,3,4,5}
--- End code ---
Then you can change the third item with this command:

--- Code: (Lua) ---a[3] = 1337
--- End code ---
Your table will then look like this:

--- Code: (Lua) ---{1,2,1337,4,5}
--- End code ---

pianoman:
Omigod that just made a game a million times easier.
Thanks!

pianoman:
How about this one: are you able to check if two tables are equal to each other?
for example:

--- Code: ---a={1,2,3}
b={1,2,4}

function on.paint(gc)
if a==b then
gc:drawString("E",0,0,"top")
else
gc:drawString("I",0,0,"top")
end

--- End code ---

Jim Bauwens:
Sadly enough you can't just compare a table like that (unless you add a metatable for it with the appropriate funcion).
You can do it like this:

--- Code: (Lua) ---
function compare(t1, t2)
    for i, p in pairs(t1) do
        if p~=t2[i] then
            return false
        end
    end
    return true
end

a={1,2,3}
b={1,2,3}

if compare(a, b) then
--they are equal
else
--they are not equal
end

--- End code ---

pianoman:
What's "p" in that code?
And what does "in pairs" mean?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version