It is currently April 20th, 2024, 5:15 am

Convert string to number

Get help with creating, editing & fixing problems with skins
User avatar
Jeff
Posts: 327
Joined: September 3rd, 2018, 11:18 am

Convert string to number

Post by Jeff »

Code: Select all

[ObviousWebParserMeasure]
Measure=String
String=00:30:00
[Hours]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\1"
[Minutes]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\2"
[Seconds]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\3"
[TimerToSeconds]
Measure=Calc
Formula=Seconds + Minutes * 60 + Hours * 3600
I've seen strings getting converted to numbers (without decimal place) before using Rainmeter, but this time it dosen't work, is there something with my RegExp?
User avatar
Jeff
Posts: 327
Joined: September 3rd, 2018, 11:18 am

Re: Convert string to number

Post by Jeff »

Well,

Code: Select all

[TimerToSeconds]
Measure=Calc
Formula=[Seconds:EscapeRegExp] + [Minutes:EscapeRegExp] * 60 + [Hours:EscapeRegExp] * 3600
DynamicVariables=1
works, which shows my regexp was the previous problem, the question is Why? now
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Convert string to number

Post by jsmorley »

Jeff wrote: February 4th, 2021, 1:29 pm

Code: Select all

[ObviousWebParserMeasure]
Measure=String
String=00:30:00
[Hours]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\1"
[Minutes]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\2"
[Seconds]
Measure=String
String=[ObviousWebParserMeasure]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(\d+)\:(\d+)\:(\d+)":"\3"
[TimerToSeconds]
Measure=Calc
Formula=Seconds + Minutes * 60 + Hours * 3600
I've seen strings getting converted to numbers (without decimal place) before using Rainmeter, but this time it dosen't work, is there something with my RegExp?
When you use the number value of measures, like you are in your Calc measure, it will literally use the number value, and will ignore any changes to the string value you make with RegExpSubstitute. You are going to need to use the string value AS a number, like this:

Code: Select all

[TimerToSeconds]
Measure=Calc
DynamicVariables=1
Formula=[Seconds] + [Minutes] * 60 + [Hours] * 3600
When you use a [SectionVariable] in a formula, that will by default use the string value, converting it to a number if it can. This will first apply the RegExpSubstitute and use the altered string, which is what you want.


Notes:

A String measure will have a number value if the string is numeric, however, that number value will be based on the original string value of the measure, and no RegExpSubstitute will be used in that context. Substitute is applied when and where the string value is "used", and doesn't in any way actually change the value of the measure itself.

The number value of all your String measures, which are all based on the string "00:30:00", will always be zero. The measure will attempt to convert the string to a valid number, but that will end up with a number value of 00, since that is the point where the string stops being numeric. The ":" in the string is not a valid numeric value, so it gets the "00", stops trying when it gets to ":", and you end up with 00, or zero...

There is no need to use the :EscapeRegExp modifier on the [SectionVariable] in this instance. That modifier might be used if you are using the string value of one measure as a component of a regular expression on another measure.
User avatar
Jeff
Posts: 327
Joined: September 3rd, 2018, 11:18 am

Re: Convert string to number

Post by Jeff »

Well, I guess this is what I deserve for having hacky string/webparser measure show numbers. Thank you very much!
jsmorley wrote: February 4th, 2021, 1:35 pm There is no need to use the :EscapeRegExp modifier on the [SectionVariable] in this instance. That modifier might be used if you are using the string value of one measure as a component of a regular expression on another measure.
I did previously try Calc=[Hours:0] to see if it worked but it only returned 0 (which now that I look at it, it's a bit more obvious why) so then I CTRL-Z-ed to the old formula I had, added the Section Variables brackets to the measures and then added the :EscapeRegExp to all the section variables only because it had RegExp in the name (I know there are no characters to escape), so that's how and why I came to that naive conclusion
jsmorley wrote: February 4th, 2021, 1:35 pm A String measure will have a number value if the string is numeric, however, that number value will be based on the original string value of the measure, and no RegExpSubstitute will be used in that context. Substitute is applied when and where the string value is "used", and doesn't in any way actually change the value of the measure itself.
This is the part that threw me under the bus, normally I have just

Code: Select all

[Numero]
Measure=String
String=00030
and I see in the About Rainmeter > Skins tab that both number and string are the same (assuming Rainmeter converts it 24/7, haven't found any part in the manual about this, only the last sentence here which conveys the same thing you said before Note:), which made me think RM does that to any string, but the RegExpSub part is the thing I didn't know

I don't really know why I'm replying after the problem is fixed, but thanks again :P