Regards
Augustas
well you're comparing two completely different things here. table.insert adds items, string.sub gets characters, string.sub can be thought of the same as tbl[1]. however, you could easily just create a variable to track the current index, and increment it each time the function is called. though I'm wondering what your use case is here? is it on a string?
it should also be noted that tbl[#tbl+1] = "foo" is actually more efficient than table.insert(tbl, "bar") but both have the same outcome.
Let's say you have a function that wants to concentrate on a table within a table within a table within a table. Rather than writing out the whole list each time you want to refer to the table at the end, you could always just set a single regular variable to the pointer of the last table, and refer to that throughout the rest of the function.
This not only reduces the length of your code, it runs faster too (as it bypasses all those repeated extra table lookups to get to the one you want).
local tTable = {
deepTable = {
deeperStillTable = {
}
}
}
local dst = tTable.deepTable.deeperStillTable
dst.derp = 2
print(tTable.deepTable.deeperStillTable.derp) --prints 2
This is pretty much what table.insert does…You misunderstood me, the most, perhaps it's me writing the question not clearly. In other terms, I want to create a function like table insert which basically does table[#table + 1] = specified variable, and this wouldn't be sometihng like table = insert(table, value), I'd like to specifically insert(table, value), which would basically do the same as table[#table + 1] = value, but shorter, it's because I have a single table with various items in it, and some of those items are unique tables as well, which have tables within them, and to keep my code not too long and tidy I don't want to do something like table.table2.table3.table4[#table.table2.table3.table4] = value.
local tTable = {}
table.insert(tTable,"derp")
print(tTable[1]) --prints derp
using tTable = table.insert would probably turn tTable to nil, i think…
local tAdd = function(table,value)
table[#table+1] = value
end
Quick example of what bomb is sayinglocal tTable = { deepTable = { deeperStillTable = { } } } local dst = tTable.deepTable.deeperStillTable dst.derp = 2 print(tTable.deepTable.deeperStillTable.derp) --prints 2
This is pretty much what table.insert does…You misunderstood me, the most, perhaps it's me writing the question not clearly. In other terms, I want to create a function like table insert which basically does table[#table + 1] = specified variable, and this wouldn't be sometihng like table = insert(table, value), I'd like to specifically insert(table, value), which would basically do the same as table[#table + 1] = value, but shorter, it's because I have a single table with various items in it, and some of those items are unique tables as well, which have tables within them, and to keep my code not too long and tidy I don't want to do something like table.table2.table3.table4[#table.table2.table3.table4] = value.using tTable = table.insert would probably turn tTable to nil, i think…local tTable = {} table.insert(tTable,"derp") print(tTable[1]) --prints derp
Alternativly, you could just make your own function like you yourself are describing, i dunno why you haven't…local tAdd = function(table,value) table[#table+1] = value end
local myTable = {}
local notMyTableCopy = myTable
local myTable = {}
local anotherTable = myTable
anotherTable.bar= "foo"
print(myTable.bar) -->> foo
Then how can I create a copy of a table when I'm defining a new table rather than the variable being a pointer to the table?
Then how can I create a copy of a table when I'm defining a new table rather than the variable being a pointer to the table?
You iterate over every item in the table and copy it to a new table.
using a pairs loop.You iterate over every item in the table and copy it to a new table.