This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
augustas656's profile picture

Variable Incrementation like Table.insert

Started by augustas656, 15 June 2014 - 01:25 AM
augustas656 #1
Posted 15 June 2014 - 03:25 AM
You know how if you use table.insert you insert the item into the table and it actually changes unlike something as string.sub("text", 1) would only return but not increment. Can I actually increment? Because I'd like to make some simplified functions for my Kit API to use that would really easen the process and I also really don't like having to use something like tbl[#tbl + 1] because I use like table's within tables withing tables within tables etc… And having to do something like that takes up a long line of code. And I like to keep my code clean.

Regards
Augustas
theoriginalbit #2
Posted 15 June 2014 - 03:31 AM
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.
Bomb Bloke #3
Posted 15 June 2014 - 03:58 AM
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).
augustas656 #4
Posted 15 June 2014 - 07:03 PM
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.

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.

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).

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
CometWolf #5
Posted 15 June 2014 - 07:27 PM
Quick example of what bomb is saying

local tTable = {
  deepTable = {
	deeperStillTable = {
	
	}
  }
}
local dst = tTable.deepTable.deeperStillTable
dst.derp = 2
print(tTable.deepTable.deeperStillTable.derp) --prints 2


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.
This is pretty much what table.insert does…

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 on 15 June 2014 - 05:27 PM
augustas656 #6
Posted 15 June 2014 - 07:40 PM
Quick example of what bomb is saying

local tTable = {
  deepTable = {
	deeperStillTable = {
	
	}
  }
}
local dst = tTable.deepTable.deeperStillTable
dst.derp = 2
print(tTable.deepTable.deeperStillTable.derp) --prints 2


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.
This is pretty much what table.insert does…

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
MKlegoman357 #7
Posted 15 June 2014 - 07:51 PM
You seem to not understand how tables actually work. Tables are not linked in any kind of form. When you create a table it is stored in memory and the table's pointer is stored in a variable. Variables DO NOT hold tables. Table's pointers are data which says where to find the table in memory. Doing this:


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 on 15 June 2014 - 05:59 PM
augustas656 #8
Posted 15 June 2014 - 09:36 PM
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?

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 on 15 June 2014 - 07:38 PM
Lignum #9
Posted 15 June 2014 - 09:38 PM
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.
augustas656 #10
Posted 15 June 2014 - 09:45 PM
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.

It would be simpler to do something like copyTable = {unpack(table)}.
CometWolf #11
Posted 15 June 2014 - 09:49 PM
That would only work if the table is numerically indexed, and dosen't contain other tables.
augustas656 #12
Posted 15 June 2014 - 09:53 PM
Seems like there is no actual way to get such a table, unless you iterate over all the possible placeholder range, which is over 2 billion. I mean, a table such as #{1, 2, 3, nil, nil, 4} = 3, it searches for a nil value. Kind of stupid I think.. P: Sadly

Regards
Augustas
CometWolf #13
Posted 15 June 2014 - 10:01 PM
Hence…
You iterate over every item in the table and copy it to a new table.
using a pairs loop.
Edited on 15 June 2014 - 08:01 PM
Bomb Bloke #14
Posted 16 June 2014 - 02:18 AM
Or, if the table is numerically indexed, table.maxn(tableName) would work too. It doesn't execute as quickly, though.

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 on 16 June 2014 - 12:20 AM