Page 1 of 1

Substring /remove kW

Posted: Wed Sep 14, 2016 3:53 am
by AntiElitz
if trying to do this:

Code: Select all

local entity = data.raw["assembling-machine"]["assembling-machine-1"]
entity.energy_usage = entity.energy_usage * factor
However this does not work, since there it is a string with "kW" in it.
So i tried substr(entity.energy_usage, 1, strlen (entity.energy_usage) - 2) to get rid of the kW, but these lua function don't work in Factorio.

Any ideas? Thx for the help!



PS: I also look for a way to increase the item input buffer of an assembling machine (how many items are grabbed in when the assembler is on idle) - help is appreaciated ;)

Re: Substring /remove kW

Posted: Wed Sep 14, 2016 4:26 am
by aubergine18
See if this works...

Code: Select all

local energy_usage = tonumber( entity.energy_usage:match('%d+') ) -- get number

entity.energy_usage = tostring( energy_usage * factor ) .. 'kW'

Re: Substring /remove kW

Posted: Wed Sep 14, 2016 2:13 pm
by AntiElitz
thx! that worked perfectly

Re: Substring /remove kW

Posted: Wed Sep 14, 2016 2:59 pm
by DedlySpyder
You can get a substring btw, it's string.sub()

Re: Substring /remove kW

Posted: Wed Sep 14, 2016 3:31 pm
by aubergine18
DedlySpyder wrote:You can get a substring btw, it's string.sub()
Only if you know the length of the numeric portion of the string... which could be different if some other mod already changed the value to bigger/smaller number.

Re: Substring /remove kW

Posted: Wed Sep 14, 2016 4:36 pm
by DedlySpyder
aubergine18 wrote:
DedlySpyder wrote:You can get a substring btw, it's string.sub()
Only if you know the length of the numeric portion of the string... which could be different if some other mod already changed the value to bigger/smaller number.
string.sub(string, 1, (#string - 2))

You had the right idea originally, just wrong syntax

Re: Substring /remove kW

Posted: Thu Sep 15, 2016 12:13 am
by Rseding91
Energy values must end with either "W" or "J". Other than that the 2nd to last character can be any one of the following: k, K, M, G, T, P ,E ,Z, Y.

So, using those rules you can parse out what the value is, remove it, do what you need to do, and then put back what ever symbols you want.