lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Iain,

Your problem lies in an over usage of parentheses. If you leave out the outer parentheses, your line does work:

(example[1])[3] = 5

Actually the parentheses are not needed here, as there is no conflict in order of operations:

example[1][3] = 5

Kind regards,

Jan-Pieter


2013/2/10 iain morland <iain@iainmorland.net>
Hello all,

The following question probably has a super simple answer, but I've not been
able to find a solution in the list archive or other online resources. It
may be that the search terms are just too common. So I apologise in advance
if this seems idiotically simple. :-)

I have a table that is structured like so:

example={{"Red",1,2},{"Green",3,4}}

I want to define a function that changes one of the numerical values stored
in one of the nested tables. E.g. I want to change 2 in the first nested
table to 5.

I'm already able to print the contents of that index by doing this in my
function:
print((example[1])[3])

However, I can't get this to work:
((example[1])[3])=5

...and I just can't see how else I should write the syntax to assign a new
value to the specific nested index.

Although if I change the table so that it doesn't contain any sub-tables,
I'm able to do this:
example[1]=5

Can anyone advise?

Thanks for reading,
Iain