[SOLVED] (Newbie) how to pass the position of a table's index instead of the index's value
Posted: Tue Feb 07, 2017 8:05 am
I'm trying to make a function that will go to a specified table, look at a specified element of said table, and change that element in a specified way.
I can write the test function with only two arguments, but it runs into a different problem.
I'm sure I can do what I'm trying to do with this function, but I just don't how or what syntax to use.
Speaking of what I'm trying to do. I hope this is good enough to convey the problem I'm encountering, but if it's not, please let me know and I'll try to clarify
Also, I'm not using table.insert because then (I think) I would need to use a loop to iterate over the indexes and the function would need an extra argument and (again I think) I would need to give each index an ID value and I'm pretty sure it would look something like this,
but all those extra values would only muddle the issue.
Also, when I previewed this post all of the comment lines in the code blocks were misaligned. I tried to straighten them out, but I might have made things worse. if it's hard to read then sorry about that (first post) :/
Code: Select all
t = {} --Here's a table.
t.a = 1 -- The first index of the table is [a] and it has a value of [1].
-- I might be misunderstanding how to use this "table.index" thing.
-- Maybe I'm supposed to use table.insert and make multiple values for one index-
-- -and then refer to the values of said index, but I'll address that at the bottom of this post.
test (t,a,1) -- Here I'll call a function (test) and feed it the table to be addressed (t).
-- Which index to change (a), and the amount to change the index by (1).
-- Side note. I don't remember if I have to define the function before I call it (I do in my actual code)-
-- -but It's easier for me to write it like this so that's what I'm doing.
function test (n1,n2,n3) -- This function is meant go to a specific table (n1).
-- Then a specific index held in the table (n2).
-- Then change said index by a specified amount (n3).
n1.n2=n1.n2+n3 -- I think I understand why this doesn't work.
end -- It goes to the table passed to the "n1" argument (t).
-- Then it will not go to the index that was passed to the "n2" argument (a)-.
-- -instead it will try to go to the index called "n2" and find nothing.
-- If I write "n1.a = n1.a+n3" then it will work.
-- However, this function needs to be general-
-- -so that it can be called to go to any table,
-- -and change any index held in said table, by any numeric value.
I can write the test function with only two arguments, but it runs into a different problem.
Code: Select all
test2 (t.a,1) -- Here I'm calling a function (test2)-
-- -and feeding the function a specific table and index (t.a),-
-- -and the amount to change the index by (1).
function test2 (n,n1) -- (n) will contain both the table and index to change,-
-- -and (n1) will be the amount to change the index by.
n=n+n1 -- In this case I seem to be feeding the value held in the [a] index-
end -- -of table [t] which is [1] to test2's argument [n].
-- So all this function is doing is just 1=1+1 (not super useful).
-- How would I make the [n] argument refer to the index of a given table-
-- -instead of the value held by said index?
Speaking of what I'm trying to do. I hope this is good enough to convey the problem I'm encountering, but if it's not, please let me know and I'll try to clarify
Also, I'm not using table.insert because then (I think) I would need to use a loop to iterate over the indexes and the function would need an extra argument and (again I think) I would need to give each index an ID value and I'm pretty sure it would look something like this,
Code: Select all
function test3 (n1,n2,n3,n4)
for i, v in ipairs (n1) do
if v.n2 == n2 and v.id == n3 then
v.n2 = v.n2 +n4
end
end
end
Also, when I previewed this post all of the comment lines in the code blocks were misaligned. I tried to straighten them out, but I might have made things worse. if it's hard to read then sorry about that (first post) :/