Variable Incrementation like Table.insert
#1
Posted 15 June 2014 - 02:25 AM
Regards
Augustas
#2
Posted 15 June 2014 - 02:31 AM
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.
#3
Posted 15 June 2014 - 02:58 AM
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).
#4
Posted 15 June 2014 - 06:03 PM
theoriginalbit, on 15 June 2014 - 02:31 AM, said:
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.
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.
Bomb Bloke, on 15 June 2014 - 02:58 AM, said:
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).
I don't exactly understand you there, in my case I'm making an alternative read function. But it's very customisable and very advanced. I have tables that branch into other tables all containing their seperate unique tools, and I'm using object-oriented programming a lot, and I don't see any other way out of it, if you are telling me a logical concept that could make it easier in my case or something I haven't learnt about lua, I don't seem to understand you. Give me an example with code, that may help if you can break down what you're saying further.
Regards
Augustas
#5
Posted 15 June 2014 - 06:27 PM
local tTable = {
deepTable = {
deeperStillTable = {
}
}
}
local dst = tTable.deepTable.deeperStillTable
dst.derp = 2
print(tTable.deepTable.deeperStillTable.derp) --prints 2
Quote
local tTable = {}
table.insert(tTable,"derp")
print(tTable[1]) --prints derp
using tTable = table.insert would probably turn tTable to nil, i think...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
Edited by CometWolf, 15 June 2014 - 06:27 PM.
#6
Posted 15 June 2014 - 06:40 PM
CometWolf, on 15 June 2014 - 06:27 PM, said:
local tTable = {
deepTable = {
deeperStillTable = {
}
}
}
local dst = tTable.deepTable.deeperStillTable
dst.derp = 2
print(tTable.deepTable.deeperStillTable.derp) --prints 2
Quote
local tTable = {}
table.insert(tTable,"derp")
print(tTable[1]) --prints derp
using tTable = table.insert would probably turn tTable to nil, i think...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
So I see, I've tested this myself, table[place] = value, will also change the value of all other linked tables, is there a short-code way also to not change, so for example I want to create an identical table, but if this way changes the values only of that table and not the copied original table? This is very unexpected and i think it's very disadventageous. I think so because changing a single table without changing value of any linked tables is more of a basic and essential, whereas it only changing the values with also the linked tables is like an advanced feature without being a basic. To not affect copied tables / linked tables would I have to t = {unpack(t2)} if t = t2 would link them? wierd...
Regards
Augustas
#7
Posted 15 June 2014 - 06:51 PM
local myTable = {}
local notMyTableCopy = myTable
does not make a new table inside the variable 'notMyTableCopy'. Instead, variable 'notMyTableCopy' gets the same table's pointer.
local myTable = {}
local anotherTable = myTable
anotherTable.bar= "foo"
print(myTable.bar) -->> foo
Edited by MKlegoman357, 15 June 2014 - 06:59 PM.
#8
Posted 15 June 2014 - 08:36 PM
Not relevant, but now I see how java's definitions of the type of variable are adventageous in this way when defining a variable. I'm not sure wether you can make a variable that points to the table the same way as in lua in java, but defining a variable type to be a table rather than a pointer or anything has an advantage that I now know, unless I'm mistaken.
Regards
Augustas
Edited by augustas656, 15 June 2014 - 08:38 PM.
#10
#11
Posted 15 June 2014 - 08:49 PM
#12
Posted 15 June 2014 - 08:53 PM
Regards
Augustas
#14
Posted 16 June 2014 - 01:18 AM
If the table contains non-numeric indexes then #tableName wouldn't work anyway.
I suppose you could serialise/unserialise and get your copy that way. That's not very efficient either, but it'd be easy to code.
It may be that you don't need a copy in the first place. If you're passing tables between recursive functions, then odds are that you don't.
Edited by Bomb Bloke, 16 June 2014 - 01:20 AM.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











