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

Changing one value in a table

Started by SmileyCraft, 30 September 2012 - 02:54 PM
SmileyCraft #1
Posted 30 September 2012 - 04:54 PM
I am making a program where i need tables, but at some parts i want to be able to change only one value of the table.

Example:

you create a table:
local var1 = {1, 2, 3, 4, 5}
now you want this table to change to this:
{1, 2, 7, 4, 5}

is there a simple way to do that?
jag #2
Posted 30 September 2012 - 04:56 PM
var1[3] = 7
Explanation:
In your table/array you got the values
1, 2, 7, 4, 5
and so each value got it's own position.
The first 1 got the position of 1, the 2 got the position 2, the 7 got 3, 4 got 4 and so forth.

So if you got another table like this
var2 = {2,5,7,3,4}
and then print a part of it
print(var1[4])
you'll get the number 3 back.
GopherAtl #3
Posted 30 September 2012 - 04:57 PM
indexing operators, [].

in your example, you changed the value of the 3rd element, so var1[3]=7 would change it from 3 to 7.

:edit: ninja'd, doh.
SmileyCraft #4
Posted 30 September 2012 - 04:58 PM
Lol that simple? XD how could i not think of that O.o