It is currently March 28th, 2024, 10:29 am

Using variables in a loop.

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Using variables in a loop.

Post 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.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post 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.
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Using variables in a loop.

Post by FreeRaider »

Maybe this:
SKIN:Bang(string.format('!WriteKeyValue Variables B%s "0" "#@#FileToEdit.txt"', i))
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post 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!
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post 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
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:Actually that worked. Thanks!
Really? I'm a bit surprised, but I'm glad in this case.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post 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?
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post 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.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post 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?
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post 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.
Post Reply