Hi all,
I have been searching the forums and Google for a couple of days and haven't been able to find anything to help me solve this issue.
What I am attempting to do is separate a variable number into individual digits. For example:
number=526.4
number1=5
number2=2
number3=6
number4=4
I have tried a lot of different ways to do it including: regexp, modular, trunc, round and none of these I am able to get to do what I am after.
Any help would be gratefully received!
It is currently September 19th, 2020, 12:52 pm
Split number into variables

 Posts: 31
 Joined: October 27th, 2012, 5:58 pm
 Location: SWUK

 Developer
 Posts: 21365
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Split number into variables
A bit more detail would be helpful. What do you intend to do with the separate values? It matters, since if they are going to be used in separate meters in the skin, some thought needs to be given to the variable length of the number.
Breaking the number up in Lua can be done several ways and is pretty easy, but which way you go can depend on what you are going to be using it for.
Breaking the number up in Lua can be done several ways and is pretty easy, but which way you go can depend on what you are going to be using it for.

 Posts: 31
 Joined: October 27th, 2012, 5:58 pm
 Location: SWUK
Re: Split number into variables
Thanks for your reply. Looking back on what I wrote, I realised that it is actually confusing even to myself. Hopefully I'll make it more understandable this time
The length of the number itself will always be three digits, a decimal point and another digit. So the length will always be four digits (five including the decimal point, but I'm guessing that wouldn't come into it). It's the individual digits that will change.
With those digits, I am hoping to turn them into bars so the length of each bar will be dependant on the digit. For example, with the number 526.4, 5 would be 50 pixels high, the 2 would be 20 pixels high, and so on.
Hope that helps.
The length of the number itself will always be three digits, a decimal point and another digit. So the length will always be four digits (five including the decimal point, but I'm guessing that wouldn't come into it). It's the individual digits that will change.
With those digits, I am hoping to turn them into bars so the length of each bar will be dependant on the digit. For example, with the number 526.4, 5 would be 50 pixels high, the 2 would be 20 pixels high, and so on.
Hope that helps.

 Developer
 Posts: 21365
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Split number into variables
Not sure I see any great advantage to Lua, assuming you aren't going to be doing this with a boatload of numbers in the skin, where a "for/next" or "called function" approach might reduce some code. With just one number, I think it's just as easy to do it in native Rainmeter. Keep in mind that this method entirely depends on there being exactly that format \d\d\d.\d.FoxyXII wrote:Thanks for your reply. Looking back on what I wrote, I realised that it is actually confusing even to myself. Hopefully I'll make it more understandable this time
The length of the number itself will always be three digits, a decimal point and another digit. So the length will always be four digits (five including the decimal point, but I'm guessing that wouldn't come into it). It's the individual digits that will change.
With those digits, I am hoping to turn them into bars so the length of each bar will be dependant on the digit. For example, with the number 526.4, 5 would be 50 pixels high, the 2 would be 20 pixels high, and so on.
Hope that helps.
Code: Select all
[MeasureNum]
Measure=Calc
Formula=573.9
[MeasureDig1]
Measure=Calc
Formula=MeasureNum
RegExpSubstitute=1
Substitute="(\d)\d\d.\d":"\1"
[MeasureDig2]
Measure=Calc
Formula=MeasureNum
RegExpSubstitute=1
Substitute="\d(\d)\d.\d":"\1"
[MeasureDig3]
Measure=Calc
Formula=MeasureNum
RegExpSubstitute=1
Substitute="\d\d(\d).\d":"\1"
[MeasureDig4]
Measure=Calc
Formula=MeasureNum
RegExpSubstitute=1
Substitute="\d\d\d.(\d)":"\1"
Having said that, if you have other valueadded processing you need to do in Lua with the digits, and want to split them in Lua instead of Rainmeter, let me know. It's certainly not hard.
You do not have the required permissions to view the files attached to this post.

 Developer
 Posts: 21365
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Split number into variables
The core of the Lua is this simple function:
posArg should be one of 1, 2, 3 or 5 in this case...
Lua is very dynamically "typed", so treating something that looks like a number as either a number or string value is completely transparent. Lua doesn't care, and leaves it up to you.
Code: Select all
function SplitNum(numArg, posArg)
return string.sub(numArg, posArg, posArg)
end
Lua is very dynamically "typed", so treating something that looks like a number as either a number or string value is completely transparent. Lua doesn't care, and leaves it up to you.

 Developer
 Posts: 21365
 Joined: April 19th, 2009, 11:02 pm
 Location: Fort Hunt, Virginia, USA
Re: Split number into variables
But at the end of the day, you are going to need four Calc measures in any case, to provide a percentage value to use with MeasureName in the Bar meters. So the advantage to Lua in this case is that you would really need eight measures to do this natively in Rainmeter, (four to use Substitute to split the string value, and four to turn that into a percentage number value) but only four if you get the value as a number instead of a string from Lua.
Skin:
Test.lua:
Skin:
Code: Select all
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
[MeasureNum]
Measure=Calc
Formula=573.9
[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1
[MeasureDig1]
Measure=Calc
Formula=[&Lua:SplitNum([&MeasureNum],1)]
MinValue=0
MaxValue=9
DynamicVariables=1
[MeasureDig2]
Measure=Calc
Formula=[&Lua:SplitNum([&MeasureNum],2)]
MinValue=0
MaxValue=9
DynamicVariables=1
[MeasureDig3]
Measure=Calc
Formula=[&Lua:SplitNum([&MeasureNum],3)]
MinValue=0
MaxValue=9
DynamicVariables=1
[MeasureDig4]
Measure=Calc
Formula=[&Lua:SplitNum([&MeasureNum],5)]
MinValue=0
MaxValue=9
DynamicVariables=1
[MeterBar1]
Meter=Bar
W=10
H=90
MeasureName=MeasureDig1
BarColor=0,255,0,255
SolidColor=47,47,47,255
[MeterBar2]
Meter=Bar
X=5R
W=10
H=90
MeasureName=MeasureDig2
BarColor=0,255,0,255
SolidColor=47,47,47,255
[MeterBar3]
Meter=Bar
X=5R
W=10
H=90
MeasureName=MeasureDig3
BarColor=0,255,0,255
SolidColor=47,47,47,255
[MeterBar4]
Meter=Bar
X=5R
W=10
H=90
MeasureName=MeasureDig4
BarColor=0,255,0,255
SolidColor=47,47,47,255
Code: Select all
function SplitNum(numArg, posArg)
return string.sub(numArg, posArg, posArg)
end
You do not have the required permissions to view the files attached to this post.