It is currently March 28th, 2024, 12:00 pm

[Suggestion] All strings that are nil, number value 0, otherwise 1

Report bugs with the Rainmeter application and suggest features.
Post Reply
User avatar
Cariboudjan
Posts: 260
Joined: May 12th, 2019, 8:55 am

[Suggestion] All strings that are nil, number value 0, otherwise 1

Post by Cariboudjan »

Maybe there's a reason why this isn't already the case, but it would be pretty useful if all nil strings had a number value of 0 and all non-nil strings had a number value of 1.

Untitled.png
Untitled2.png
Use case might be to check the number of non-nil strings in a line, so you could, for example use:

Code: Select all

[Variables]
String1=Hello
String2=
String3=World
String4=You go girl
String5=

[StringMeasure1]
Measure=String
String=#String1#

[StringMeasure2]
Measure=String
String=#String2#

[StringMeasure3]
Measure=String
String=#String3#

[StringMeasure4]
Measure=String
String=#String4#

[StringMeasure5]
Measure=String
String=#String5#

[Calc]
Measure=Calc
Formula=[StringMeasure1:]+[StringMeasure2:]+[StringMeasure3:]+[StringMeasure4:]+[StringMeasure5:]
Output = 3
User avatar
Yincognito
Rainmeter Sage
Posts: 7017
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [Suggestion] All strings that are nil, number value 0, otherwise 1

Post by Yincognito »

I believe the reason is simply that any non numerical string converted to a number is 0 in most if not all programming languages.

As for the use case, that can be done in just 2 measures via simple substitutions - let me know if you're interested or you need it ASAP.
User avatar
Brian
Developer
Posts: 2673
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: [Suggestion] All strings that are nil, number value 0, otherwise 1

Post by Brian »

I don't think this will be possible for 2 reasons.

First, when measure values are retrieved, strings are preferred over numeric values (in most cases). If we automatically used "nil" as an indicator of "something", then no numeric values would ever be displayed when a numeric value was specifically asked for. Most of the time, if a string value converts to a number, the number value will also be set.

Second, I think this would have the potential to break a lot of existing skins.

An alternative to Yincognito's solution could be to use a lua script that would send in multiple string values and output the sum of existing number of valid strings.

-Brian
User avatar
Yincognito
Rainmeter Sage
Posts: 7017
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [Suggestion] All strings that are nil, number value 0, otherwise 1

Post by Yincognito »

Cariboudjan wrote: March 24th, 2022, 4:08 pm[...] Use case might be to check the number of non-nil strings in a line [...]
Brian wrote: March 25th, 2022, 4:53 amAn alternative to Yincognito's solution could be to use a lua script that would send in multiple string values and output the sum of existing number of valid strings.
Here are both variants...

@Resources\Script.lua:

Code: Select all

function Count(...)
  local count = 0
  for i = 1, #arg do if arg[i]~='' then count = count + 1 end end
  return count
end
Skin.ini:

Code: Select all

[Variables]
String1=Hello
String2=
String3=World
String4=You go girl
String5=

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

; Ini method
[Formula]
Measure=String
String=#String1#,#String2#,#String3#,#String4#,#String5#,
UpdateDivider=-1
RegExpSubstitute=1
Substitute="^,":"",",+":",",".+?,":"+1"
DynamicVariables=1

[Result]
Measure=Calc
Formula=[Formula]
UpdateDivider=-1
DynamicVariables=1

; Lua method
[Script]
Measure=Script
ScriptFile=#@#Script.lua
UpdateDivider=-1

---Meters---

[MeterTest]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
Text="Non Empty Strings (Ini) = [Result:]#CRLF#Non Empty Strings (Lua) = [&Script:Count('#String1#','#String2#','#String3#','#String4#','#String5#')]"
UpdateDivider=-1
DynamicVariables=1
FullStrings.jpg
References: Function Definitions In Lua 5.1
Post Reply