It is currently April 12th, 2021, 4:32 am

Getting the maximum value of "n" variables

Help with creating, editing & fixing problems with skins
User avatar
jsmorley
Developer
Posts: 21614
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Getting the maximum value of "n" variables

Post by jsmorley »

Someone on IRC had this question, and this is how I would likely approach it:

Skin:

Code: Select all

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

[Variables]
Var1=2
Var2=15
Var3=10
Var4=7

[Lua]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=-1

[MeterMax]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Maximum Variable Value is: [&Lua:MaxValue(#Var1#, #Var2#, #Var3#, #Var4#)]
DynamicVariables=1
Test.lua:

Code: Select all

function MaxValue(...)

	valueTable = {}
	
	for i = 1, #arg do
		table.insert(valueTable, arg[i])
	end
	
	table.sort(valueTable, function(a,b) return a>b end)
	
	return valueTable[1]

end
1.png
So you can call that MaxVaue() function anywhere you need it, in a String meter, or a Calc measure, and pass it any number of numeric values. It will return the maximum value of the set you pass it.
You do not have the required permissions to view the files attached to this post.
areking
Posts: 2
Joined: December 19th, 2017, 2:03 pm

Re: Getting the maximum value of "n" variables

Post by areking »

Thanks, it works perfectly.
Now another question: if i wanted to use as variables some Meter and Measure values, how do i do that in Lua?
I mean i have this:

Code: Select all

[Meter1]
Meter=String
Text=Meter1
X=0
Y=0

[Meter2]
Meter=String
Text=Meter2
X=0
Y=R
Now let's say i want to find the Width or the Height or the Y of these meters
How do i put these parameters as the variables in the lua script?
User avatar
jsmorley
Developer
Posts: 21614
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting the maximum value of "n" variables

Post by jsmorley »

areking wrote:Thanks, it works perfectly.
Now another question: if i wanted to use as variables some Meter and Measure values, how do i do that in Lua?
I mean i have this:

Code: Select all

[Meter1]
Meter=String
Text=Meter1
X=0
Y=0

[Meter2]
Meter=String
Text=Meter2
X=0
Y=R
Now let's say i want to find the Width or the Height or the Y of these meters
How do i put these parameters as the variables in the lua script?
You can pass the Width or Height or X or Y of the meters to that function the same way you would pass the variables in my example...

Code: Select all

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

[Lua]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=-1

[MeterOne]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Hello world

[MeterTwo]
Meter=String
Y=0R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Every good boy deserves favor

[MeterThree]
Meter=String
Y=0R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Hi there

[MeterMax]
Meter=String
Y=10R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Maximum Meter Width is: [&Lua:MaxValue([&MeterOne:W], [&MeterTwo:W], [&MeterThree:W])]
DynamicVariables=1
1.png
However, this doesn't tell you "which" meter is the longest, if that is what you are going for.
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 12543
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Getting the maximum value of "n" variables

Post by balala »

Probably the number of variables is limited, but if this number doesn't change continuously, a .lua file isn't needed:

Code: Select all

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

[Variables]
Var1=65
Var2=25
Var3=69
Var4=74
Var5=1
Var6=51
Var7=40
Var8=7
Var9=12
Var10=39
Var11=77
Var12=11
Var13=37
Var14=82
Var15=75
Var16=72
Var17=39
Var18=93
Var19=92
Var20=74
Var21=110
Var22=55

[MeasureMax]
Measure=Calc
Formula=( Max ( #Var1#, ( Max ( #Var2#, ( Max ( #Var3#, ( Max ( #Var4#, ( Max ( #Var5#, ( Max ( #Var6#, ( Max ( #Var7#, ( Max ( #Var8#, ( Max ( #Var9#, ( Max ( #Var10#, ( Max ( #Var11#, ( Max ( #Var12#, ( Max ( #Var13#, ( Max ( #Var14#, ( Max ( #Var15#, ( Max ( #Var16#, ( Max ( #Var17#, ( Max ( #Var18#, ( Max ( #Var19#, ( Max ( #Var20#, ( Max ( #Var21#, #Var22# ))))))))))))))))))))))))))))))))))))))))))

[MeterMax]
Meter=String
MeasureName=MeasureMax
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Maximum Variable Value is: %1
DynamicVariables=1
This code works with 22 variables and I don't know what the limit would be, but I think further variables also can be added.
areking
Posts: 2
Joined: December 19th, 2017, 2:03 pm

Re: Getting the maximum value of "n" variables

Post by areking »

Nice, so just call them normally
Thanks, that's what i wanted
User avatar
jsmorley
Developer
Posts: 21614
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting the maximum value of "n" variables

Post by jsmorley »

balala wrote:This code works with 22 variables and I don't know what the limit would be, but I think further variables also can be added.
The maximum number of nested conditions in a calc is 30.

Sorry, but I do not agree with that hideous formula to avoid some very simple Lua...
User avatar
fonpaolo
Moderator
Posts: 1388
Joined: April 11th, 2013, 8:08 pm
Location: Italy

Re: Getting the maximum value of "n" variables

Post by fonpaolo »

I was thinking of posting a solution like balala's formula... :lol:
Not because I hate lua... :17denial
But, until now, I never found a reason to use it, in place of some formulas.
User avatar
jsmorley
Developer
Posts: 21614
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting the maximum value of "n" variables

Post by jsmorley »

fonpaolo wrote:I was thinking of posting a solution like balala's formula... :lol:
Not because I hate lua... :17denial
But, until now, I never found a reason to use it, in place of some formulas.
Sorry, but aside from the obvious likelihood of getting one or more parentheses wrong in that blizzard of a formula, it's just not anywhere near as flexible and reusable as the Lua function. You can pass any number of any numeric values to it and it will return the maximum value. It can be used for different things in different way anywhere in the skin.
User avatar
balala
Rainmeter Sage
Posts: 12543
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Getting the maximum value of "n" variables

Post by balala »

jsmorley wrote:The maximum number of nested conditions in a calc is 30.
It seems not 30, but 32. This also works:

Code: Select all

Formula=( Max ( #Var1#, ( Max ( #Var2#, ( Max ( #Var3#, ( Max ( #Var4#, ( Max ( #Var5#, ( Max ( #Var6#, ( Max ( #Var7#, ( Max ( #Var8#, ( Max ( #Var9#, ( Max ( #Var10#, ( Max ( #Var11#, ( Max ( #Var12#, ( Max ( #Var13#, ( Max ( #Var14#, ( Max ( #Var15#, ( Max ( #Var16#, ( Max ( #Var17#, ( Max ( #Var18#, ( Max ( #Var19#, ( Max ( #Var20#, ( Max ( #Var21#, ( Max ( #Var22#, ( Max ( #Var23#, ( Max ( #Var24#, ( Max ( #Var25#, ( Max ( #Var26#, ( Max ( #Var27#, ( Max ( #Var28#, ( Max ( #Var29#, ( Max ( #Var30#, ( Max ( #Var31#, #Var32# ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
But with 33 variables, it doesn't.
jsmorley wrote:Sorry, but I do not agree with that hideous formula to avoid some very simple Lua...
Well, I didn't say my solution would be better or more preferable. I just said that at least in some cases, the lua solution isn't needed. I think depends on each one, what solution do prefers...
User avatar
jsmorley
Developer
Posts: 21614
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting the maximum value of "n" variables

Post by jsmorley »

Doesn't even have to be "numeric". It will sort strings alphabetically just as easily... Exactly the same Lua function...

Code: Select all

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

[Variables]
Var1=AAA
Var2=ZAB
Var3=ZZZ
Var4=RRR

[Lua]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=-1

[MeterMax]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Maximum alphabetical value is: [&Lua:MaxValue('#Var1#', '#Var2#', '#Var3#', '#Var4#')]
DynamicVariables=1
1.png
You do not have the required permissions to view the files attached to this post.