It is currently April 20th, 2024, 9:07 am

Pass in string then concatenate array name in lua

Get help with creating, editing & fixing problems with skins
User avatar
Jax
Posts: 104
Joined: June 7th, 2021, 11:46 am

Pass in string then concatenate array name in lua

Post by Jax »

I wanted to make it so that I can get the array by passing a number in a function.

Bang: [!CommandMeasure Lua "loadArray(1)"]

Lua:

Code: Select all

1Array = {0}

loadArray(num)
	print(num..Array[0])
end
:confused:
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Pass in string then concatenate array name in lua

Post by balala »

Jax wrote: August 15th, 2021, 9:47 am I wanted to make it so that I can get the array by passing a number in a function.

Bang: [!CommandMeasure Lua "loadArray(1)"]

Lua:

Code: Select all

1Array = {0}

loadArray(num)
	print(num..Array[0])
end
:confused:
In lua the indices of arrays by default are starting from 1, not from 0. So the above print function should be print(num..Array[1]), in order to get concatenated the num variable with the first element of the Array array.
Note that there seems to be a typo in the above code. I suppose the array should be Array = {0} (not 1Array = {0}). If I'm not mistaken the name of an array can't start with a number in lua.
User avatar
Jax
Posts: 104
Joined: June 7th, 2021, 11:46 am

Re: Pass in string then concatenate array name in lua

Post by Jax »

balala wrote: August 15th, 2021, 10:34 am If I'm not mistaken the name of an array can't start with a number in lua.
I see...

but how can I solve my problem? supposed I have 2 arrays, array1 and array2, when I run [!CommandMeasure Lua "loadArray(1)"] I want to make it print out the 1st value of array1. :confused:
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Pass in string then concatenate array name in lua

Post by death.crafter »

Jax wrote: August 15th, 2021, 10:39 am I see...

but how can I solve my problem? supposed I have 2 arrays, array1 and array2, when I run [!CommandMeasure Lua "loadArray(1)"] I want to make it print out the 1st value of array1. :confused:
You can't... It's a variable so it should be accessed by name only.

And also you can't start a variable name from 1. Well, if lua follows usual programming rules.

But you can do:

Code: Select all

local mainArray={}
mainArray["subArray1"]={'cat', 'dog'}
mainArray["subArray2"]={'crow', 'pigeon'}

Code: Select all

!CommandMeasure Lua "print(mainArray['subArray'..'#NumberVariable#'][1])"
Or make it a function. Whatever you prefer.
from the Realm of Death
User avatar
Jax
Posts: 104
Joined: June 7th, 2021, 11:46 am

Re: Pass in string then concatenate array name in lua

Post by Jax »

Cool, thanks :thumbup:
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Pass in string then concatenate array name in lua

Post by jsmorley »

A simple example of obtaining a value from a Lua table (array) by the implied index number:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureLoop]
Measure=Loop
StartValue=1
EndValue=4

[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterOutput]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[&Lua:Lookup([&MeasureLoop:])]
DynamicVariables=1
Test.lua:

Code: Select all

function Initialize()

	tableLookup = {'one banana','two banana','three banana', 'four'}

end

function Lookup(indexArg)
	
	return tableLookup[indexArg]
		
end

Table indexes in Lua start with "1", not "0". Keep this in mind if you are used to zero-based arrays in other languages.

Also keep in mind that you cannot directly access the indexed value of a table in Lua from Rainmeter

Text=[&Lua:tableLookup[[&MeasureLoop:]] won't work with tables like it would with just a "variable" set in Lua. You have to pass the index number you want, and return that using a Lua call to the table values.


Animation.gif


You can add more functionality to react to "mistakes" as well:

Code: Select all

function Initialize()

	tableLookup = {'one banana','two banana','three banana', 'four'}

end

function Lookup(indexArg)
	
	if indexArg > #tableLookup or indexArg < 1 then
		return 'Out of range!'
	else
		return tableLookup[indexArg]
	end

end
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Pass in string then concatenate array name in lua

Post by Yincognito »

By the way, if you want to print the whole array, you can use the unpack() function in Lua:

Code: Select all

print(unpack{10,20,30})
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth