Community discussions

MikroTik App
 
patrickmkt
Member Candidate
Member Candidate
Topic Author
Posts: 212
Joined: Sat Jul 28, 2012 5:21 pm

Remove unit from GPS data - string operators - variable type

Wed Apr 23, 2025 12:15 am

Hi,
I am struggling with a very simple task.
I am trying to get the value of the GPS altitude.
local Gps [ /system/gps/monitor once as-value ];
/log/info ($Gps->"altitude");
It will return "1.480000 m"

Now I'm trying to get the altitude value without the unit.
local Gps [ /system/gps/monitor once as-value ];
/log/info (pick ($Gps->"altitude") 0 len(($Gps->"altitude")-2));
But I get the following result: "00:01:29" instead. It seems that it was transtyped as a time instead of string or a number...

How can I trim the measurement unit and keep only the value as a number (or string)?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 13099
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove unit from GPS data - string operators - variable type

Wed Apr 23, 2025 2:47 am

Bad assumptions: RouterOS do not support decimal numbers, only integer, so, only string if dot is present,
or convert (using strings splitting at the dot) 1.480000 m to 1480 mm or 1480000 µm

Use correct syntax, is full of errors and omissions.

correct example code

:local monGPS           [/system gps monitor once as-value]
:local fullAltitude     ($monGPS->"altitude")
:local unitlessAltitude [:pick $fullAltitude 0 [:find $fullAltitude " " -1]]
/log info $unitlessAltitude

your wrong way fixed code

:local xGps [/system gps monitor once as-value]
/log info [:pick ($xGps->"altitude") 0 ([:len ($xGps->"altitude")] - 2)]
 
patrickmkt
Member Candidate
Member Candidate
Topic Author
Posts: 212
Joined: Sat Jul 28, 2012 5:21 pm

Re: Remove unit from GPS data - string operators - variable type

Wed Apr 23, 2025 3:04 am

Thanks