Page 1 of 3

Using variables in a loop.

Posted: April 29th, 2018, 2:42 pm
by kyriakos876
Hello, in this code:

Code: Select all

	
	NextAvailable = {} 
	local flag = '0'

	for i = 1, 100 do
   
		table.insert(NextAvailable , SKIN:GetVariable('B'..i))
		if NextAvailable[i] == '1' then 
		
			flag = '1'
			SKIN:Bang(string.format('!WriteKeyValue Variables B%s "0" "#@#FileToEdit.txt"', #NextAvailable))
			
			break 
		end
	end
I want to use the current i instead of the #NextAvailable (as this is a table and not a number) but when I write this: SKIN:Bang(string.format('!WriteKeyValue Variables B%s "0" "#@#FileToEdit.txt"', #i)) it doesn't work as it reports a syntax error. What would the correct syntax be in order to write something in the variable B#i#?

-Thanks in advance.

Re: Using variables in a loop.

Posted: April 29th, 2018, 2:55 pm
by balala
If the BXX variables are definitely numbers, try to convert the got variable to number, using the tonumber function: table.insert(NextAvailable , tonumber(SKIN:GetVariable('B'..i))).
If this doesn't fix the issue (and probably it doesn't), please pack the config and upload it, to can check what's going on.

Re: Using variables in a loop.

Posted: April 29th, 2018, 3:15 pm
by FreeRaider
Maybe this:
SKIN:Bang(string.format('!WriteKeyValue Variables B%s "0" "#@#FileToEdit.txt"', i))

Re: Using variables in a loop.

Posted: April 29th, 2018, 3:18 pm
by kyriakos876
balala wrote:If the BXX variables are definitely numbers, try to convert the got variable to number, using the tonumber function: table.insert(NextAvailable , tonumber(SKIN:GetVariable('B'..i))).
If this doesn't fix the issue (and probably it doesn't), please pack the config and upload it, to can check what's going on.
Actually that worked. Thanks!

Re: Using variables in a loop.

Posted: April 29th, 2018, 3:20 pm
by kyriakos876
FreeRaider wrote:Maybe this:
SKIN:Bang(string.format('!WriteKeyValue Variables B%s "0" "#@#FileToEdit.txt"', i))
Yes, I tried that but apparently I needed the tonumber command. ^_^

Now that's how it works. #i was wrong and I used it as an example :D

Re: Using variables in a loop.

Posted: April 29th, 2018, 3:27 pm
by balala
kyriakos876 wrote:Actually that worked. Thanks!
Really? I'm a bit surprised, but I'm glad in this case.

Re: Using variables in a loop.

Posted: April 29th, 2018, 3:44 pm
by kyriakos876
balala wrote:Really? I'm a bit surprised, but I'm glad in this case.
Yea, just so I'm sure I understand what happened here, does the tonumber command converts everything to numbers? And I suppose that in my table everything was strings?

Re: Using variables in a loop.

Posted: April 29th, 2018, 6:02 pm
by balala
kyriakos876 wrote:does the tonumber command converts everything to numbers?
Everything which is possible. There is no way to convert for example an a to number, but if a string is composed from digits, then yes, it is converted to number.
kyriakos876 wrote:And I suppose that in my table everything was strings?
Probably.

Re: Using variables in a loop.

Posted: April 29th, 2018, 7:40 pm
by kyriakos876
Thanks for clarifying that,

Now another issue occurred,

SKIN:Bang(string.format('!WriteKeyValue Variables B%s "1" #@#FileToEdit.txt', LastAdded))

this doesn't work but as soon as I remove the filetowrite parameter, it works normally. What could be causing that?

Re: Using variables in a loop.

Posted: April 29th, 2018, 7:54 pm
by balala
kyriakos876 wrote:SKIN:Bang(string.format('!WriteKeyValue Variables B%s "1" #@#FileToEdit.txt', LastAdded))

this doesn't work but as soon as I remove the filetowrite parameter, it works normally. What could be causing that?
Try this: SKIN:Bang(string.format('!WriteKeyValue Variables B%s "1" [color=#FF0000]"[/color]#@#FileToEdit.txt[color=#FF0000]"[/color]', LastAdded))
As you can see I included the file into quotations. This will help especially if the file path contains spaces. But even if it doesn't, it worth a try.
Please let me know if it did help.