Page 1 of 1

lua pass by reference issue

Posted: Thu Nov 02, 2017 7:34 pm
by CmdrKeen
I was under the impression that variable assignment was pass by reference for tables. However, I'm finding an issue with the following:

Code: Select all

	parse["1"]["inner"]["1"] = some_number
works properly, but the code as handled by a for loop:

Code: Select all


local values = ...
for _,v in ipairs(values) do
	parse= parse[v]
end
parse = some_number
fails to assign properly. Is there a way to parse through such a table without losing the reference?

Re: lua pass by reference issue

Posted: Thu Nov 02, 2017 7:48 pm
by Rseding91
Passing a table around is passed by reference. Values are always values and never references.

Re: lua pass by reference issue

Posted: Thu Nov 02, 2017 9:13 pm
by CmdrKeen
I guess my next question is parse[v] evaluated as a value then?

Re: lua pass by reference issue

Posted: Thu Nov 02, 2017 9:24 pm
by Rseding91
You're setting the value of parse to the sub value of parse[v] so if any of [v] evaluates to a value or nil then yes it will be a value.