It is currently April 25th, 2024, 12:36 am

Get the length of any string values. (An alternative way)

Tips and Tricks from the Rainmeter Community
User avatar
LGP123
Posts: 60
Joined: November 5th, 2016, 12:15 pm

Get the length of any string values. (An alternative way)

Post by LGP123 »

When you want to get the length of a string (so the amount of character), you would probably have :

Code: Select all

; skin.ini file
[MeasureString]
Measure=String
String=AStringValue
OnUpdateAction=!CommandMeasure "MeasureScript" "StringLength([MeasureString])"

[MeasureScript]
Measure=Script
ScriptFile=Script.lua

Code: Select all

; Script.lua file
function StringLength(a)
	b = string.len(a)
	print(b)
end
The issue here is that it will only work if the string value of MeasureString is a number since we can only call a function with a integer variable.

So here is my workaround wich works for any string value, of any length. It takes 2 updates (1 update after the change of the string measure)

Code: Select all

; skin.ini file
[MeasureString]
Measure=String
String=AStringValue

[MeasureStringLengthSub]
Measure=String
String=[MeasureString]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".":"+1","^(.+)":"0\1"

[MeasureCalc]
Measure=calc
Formula=[MeasureStringLengthSub]
DynamicVariables=1
OnChangeAction=!log [MeasureCalc]
So to explain briefly, we first get the string value of MeasureString and use Reguliar Expression Substitution to first change any character to "+1", and then add a 0 at the beginning. All of this inside MeasureStringLengthSub.
We then get the string value of MeasureStringLengthSub and compute it with MeasureCalc.

Hope i didn't make any mistakes, and hope it will be usefull in some ways :).
Last edited by LGP123 on July 31st, 2018, 10:27 pm, edited 4 times in total.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Get the length of any string values.

Post by balala »

LGP123 wrote:Hope i didn't make any mistakes,
Unfortunately you did, I noticed two:
  • You forgot to add the "type" of the section, on the [MeasureString] measure: Measure=String (on both pieces of code).
  • In the [MeasureStringLengthSub] measure of the last code, there is a typo in the String option. The String=[color=#FF0000]{[/color]MeasureString] option should have to be String=[color=#22FF22][[/color]MeasureString].
And finally a workaround for your initial code (obviously first you have to fix the first issue described above). You simply have to replace the OnUpdateAction of the [MeasureString] measure, with the following one: OnUpdateAction=[!CommandMeasure "MeasureScript" [color=#FF0000]""[/color]"StringLength([color=#FF0000]"[/color][MeasureString][color=#FF0000]"[/color])"[color=#FF0000]""[/color]]. I marked here the newly added characters, excepting the brackets, which are optional, but seems a good behavior to use them. With this small modification, the code works even for strings.
Don't misunderstand me: I don't want to say your (last) solution wouldn't be good enough. Just figured out this solution and wanted to share it. Your workaround can help many of us, I think.
User avatar
LGP123
Posts: 60
Joined: November 5th, 2016, 12:15 pm

Re: Get the length of any string values.

Post by LGP123 »

balala wrote:OnUpdateAction=[!CommandMeasure "MeasureScript" [color=#FF0000]""[/color]"StringLength([color=#FF0000]"[/color][MeasureString][color=#FF0000]"[/color])"[color=#FF0000]""[/color]]
First thanks for the fix :3, that's what happens when you write a code directly on the post.

So you are saying that the reason why a call for a fonction with a string didn't work is because that if there aren't any quotes, it is computed as (lua's) variable. well that makes sense. It is good to know.
I don't know if it is better for rainmeter's performance (calling a plugin) than just using measures. Probably.
Maybe I'll stress test this later on. :thumbup:
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Get the length of any string values.

Post by balala »

LGP123 wrote:First thanks for the fix :3, that's what happens when you write a code directly on the post.
This never is a good idea, I think.
LGP123 wrote:So you are saying that the reason why a call for a fonction with a string didn't work is because that if there aren't any quotes, it is computed as (lua's) variable. well that makes sense. It is good to know.
I'm not sure, because my experience with the programming languages (any of them) is quite limited, but I think this is correct.
However with the solution I've posted, the string can include even space(s). This is not a problem.
LGP123 wrote:I don't know if it is better for rainmeter's performance (calling a plugin) than just using measures. Probably.
Can't say this.
User avatar
LGP123
Posts: 60
Joined: November 5th, 2016, 12:15 pm

Re: Get the length of any string values. (An alternative way)

Post by LGP123 »

Ok so I made a stress test. I used a string measure that can be changed with a left mouse down meter. Here are my results
Using 15k string length measure set (so 30k measures), It uses up to 5% of my computer's cpu perf over ~2sec.
But it takes like 1min 30sec just to load the skin.

Otherwise, I tried to call 15k times a luascript function that only computes the string length. To do this I just put 15 bangs that call the function with the string value.
So, unlike using the measures, it loads almost instantly, and when I activate the bangs it just computes it instantly and takes about 0.9% of my cpu.
That was really interesting to me how good it was so I tried with 150k bangs :3. Atom was even lagging with that large amount of char. And so I ran the bangs and raimeter just computed them Instantly. It used maybe like 3% of my cpu. That's just astounding :17what . Rainmeter's luascript seems pretty optimized. (Or maybe we can't just call multiple times a function within a hudge bang)