Tables

I have 2 questions.

Firstly, how do i assign keys and vars in a table?

Secondly, is this correct:

mytable = { "k", "v" }

Thanks for any help.

[lua]-- Sample table code
local mytable = {}
mytable[1] – Key = 3.14 – Value
mytable[“two”] – Key = “A string!” – Value

local mytable2 = {
3.14, – Value, automatically has key of 1
“two” = “A string!”, – Key of “two”, value of “A string!”
}
[/lua]
So you can assign keys and values either when you create the table or later on.

Yes your table is correct.

Ok, one more thing, is this correct:

mytable = { “key” = "value, “keyone” = “valueone” }

or

mytable = { “key”, “value”, “key1”, “value1” }

mytable = { “key” =“value”, “key2” = “value2” }

Your second example is four values with keys of 1, 2, 3, and 4, respectively.

Ok thanks very much.

how do i find out what line the key and var are actually on?

function hello(ply)

local x = { “hello”, “pie”, “delta”, “money” }

for k, v in pairs( mytable ) do

if v == ply then return end

if k == myhead then return end

else table.remove( x, whatever line k and v are on? )

end

(This is not thread hijacking because we are making this addon together so the problem is shared…)

[lua]function Hello(ply)
local mytable = {“hello”, “pie”, “delta”, “money”}
for k, v in pairs (mytable) do
if v != ply and k != myhead then
table.remove(mytable, k)
end
end[/lua]

Never use return in a for statement unless it is supposed to end the whole function.

thanks :slight_smile: but should i not use ~= instead of != i was told something like != means if its the opposite and ~= means if its not although its never effected me :stuck_out_tongue:

just a technical Q :slight_smile:

EDIT:
table.insert(mytable, car = ply) is that how to insert a key AND a var? thanks :slight_smile:

Does anyone know? I still can’t figure out this table stuff. Thanks

table.insert(table,value)

Just what it says; it “inserts” a value as an integer key one higher than the max. You can’t give it a key yourself. So if your table was:


local tableofstuff = {
"A string",
10,
false}

And you did table.insert(tableofstuff,“A second string”) then that string has a key of 4.