[SOLVED] (Newbie) how to pass the position of a table's index instead of the index's value

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
CinderRose
Prole
Posts: 7
Joined: Mon Feb 06, 2017 3:44 am

[SOLVED] (Newbie) how to pass the position of a table's index instead of the index's value

Post by CinderRose »

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.

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? 		
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,

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
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) :/
Last edited by CinderRose on Tue Feb 07, 2017 9:29 am, edited 2 times in total.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by bgordebak »

You can use n1[n2] I think. In Lua passing a variable to a table, is made like this, AFAIK.

Edit:If you use quotes like this: n1["n2"], it is the same as n1.n2, but without quotes, n2 is a variable.
CinderRose
Prole
Posts: 7
Joined: Mon Feb 06, 2017 3:44 am

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by CinderRose »

bgordebak wrote:You can use n1[n2] I think. In Lua passing a variable to a table, is made like this, AFAIK.
Error: Attempted to preform arithmetic on a nil value.

I think it has the same problem where the code is looking for the index [n2] instead of the index [a] (which is supposed to be stored in the functions n2 argument).
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by bgordebak »

BTW, try to use more understandable variable names. Using n1 for a table, and n2 for a value will just make your code unreadable. I will try your code in a minute.
CinderRose
Prole
Posts: 7
Joined: Mon Feb 06, 2017 3:44 am

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by CinderRose »

bgordebak wrote:BTW, try to use more understandable variable names. Using n1 for a table, and n2 for a value will just make your code unreadable. I will try your code in a minute.
I am terrible at coming up with variable names, but I try to balance that out by commenting the hell out of the code.

Also, thanks for helping.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by bgordebak »

The problem is you are giving the value of "a" = 1 then passing an 'a' variable. You should pass it like this:

test(n1, "a", 1)

or equivalent of this. a is a nil value.
Last edited by bgordebak on Tue Feb 07, 2017 9:21 am, edited 1 time in total.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by bgordebak »

You can make a variable equal to "a" for example, and then pass that.

n2 = "a"
test(t, n2, 1)
CinderRose
Prole
Posts: 7
Joined: Mon Feb 06, 2017 3:44 am

Re: (Newbie) how to pass the position of table's index instead of the index's value

Post by CinderRose »

bgordebak wrote:The problem is you are giving the value of "a" = 1 then passing an 'a' variable. You should pass it like this:

test(n1, "a", 1)
It works! I've been trying to figure out how to do this for... too long. Thank you so very much.

Also,
bgordebak wrote:You can make a variable equal to "a" for example, and then pass that.
n2 = "a"
test(t, n2, 1)
I will keep this in mind. Again, Thank you! :)
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: [SOLVED] (Newbie) how to pass the position of a table's index instead of the index's value

Post by bgordebak »

No problem!
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: [SOLVED] (Newbie) how to pass the position of a table's index instead of the index's value

Post by bgordebak »

For future reference, your "a" is your field name. t.a equals to t["a"], and t.a = 1 means your field "a" carries 1 value. It's like t = {"a" = 1}

So, naming variables and field names understandably is important. Since you're a newbie, once your code is thousands of lines, you'll see that reading it will be very hard.

For example you can pass the variables like this, so your code is more readable:

test(table, fieldname, increment)

This is good practice. Just saying. I don't want to sound arrogant, sorry.

Edit: I wasn't developing with LÖVE for long, but I've been programming for very long. And this is what I've been told when I was a newbie. Jut wanted to mention it. Don't mind me very much.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests