compare 2 strings with numbers

Hello ,
i’m trying to comare 2 numbers but I get this error:

Script Error: cannot compare if string is less than string

but both strings have the same length

:global NewLong "34.845";
:global TestLong "35.000"



:if ($NewLong < $LimitLong) do={:log warning "all is OK"} else={:log warning "something was wrong!";}

what am I missing ?

Thanks ,

http://wiki.mikrotik.com/wiki/Manual:Scripting#Data_types
When you try run a boolean operator on a string such as “<”, it is not comparing the value of a string, but some other attribute of that string, that’s because strings do not have a concept of a number.

Now you can convert a string to a number and compare values that way like this

:set number [:tonum $number]

, but Mikrotiks number variable type does not support floating point numbers, it can only accept whole numbers 1, 25, 58 etc, not 1.02 like in your example.

So the question is, where are you getting that number in the script from? There are a few things that you can try.

1.) Break each string into two numbers, the whole number and the decimal number. If the whole numbers are equal then compare the decimal numbers to determine the greater value. More logic to this one, but should be the most reliable method.
2.) Strip the “.” from the string then convert it to a number and compare the values, but this could be an issue since your received number may have a different location for the decimal point at times, so could throw off the comparison.
3.) You can try converting the number to time with :totime, as it does seem to work, but I’m not sure if it will work in every case, and so could also cause problems.

I will explain what I need to do
I have a router with GPS
the router is used in 2 places
I want to change some setting when the router go from palce to another
Place 1 - SSID1,DHCP1,Hotspot1
Place 2 - SSID2,No-Hotspot,DHCP2

so I want to take the GSP and compare it to a known point in the middle
so do you think “:tonum” will work?

I will try , but maybe there is somwthing else to compare 2 GPS point?

Thanks ,

Then you will need to do the first option, and :tonum will work as long as you can break out the period between the numbers. To do that you will need to do some logic on the string to get what you want.

http://wiki.mikrotik.com/wiki/Manual:Scripting#Commands

First step will be getting the length of the string with :len.
Then search for the position of “.” within the string with :find.
Grab all characters before the “.” with pick, and do the same with all numbers after the “.”
Assign both of those numbers to a variable.
Do the needed comparisons.

ok , I understand it going to be some work .
I have a question -
why do I need to know the length ?
how it will help me?
if I know that the coordinates ate allways xx.yyy

Thanks ,

Grab the integer part, multiply by 1000 and add the decimal part, both converted to numbers.
Then just compare integers.

I did what you told me
do you think it will work ?

:global newlatlink;
:global newlonglink;
## Take the Long$Lat from the GPS ##
:global InterLat [:pick $newlatlink 1 3];
:global InterLong [:pick $newlonglink 1 3];
:global DeciLat [:pick $newlatlink 4 8];
:global DeciLong [:pick $newlonglink 4 8];
:global NewLat1 [($InterLat*1000)];
:global NewLong1 [($InterLong*1000)];
:global CompareLat [($NewLat1+$DeciLat)];
:global CompareLong [($NewLong1+$DeciLong)];
:set CompareLat [:tonum $CompareLat];
:set CompareLong [:tonum $CompareLong];

##Cahnge this to the Lat&Long of the changing point  ##
:global LimitLat "44815";
:global LimitLong "27148";


## if the unit is lower from the point - do Mode1 , else if the unit is uper then the point - do Mode2 ##
:if (($CompareLat > $LimitLat)&&($CompareLong > $LimitLong)) do={:log warning "Change to Mode1"; :global Mode 1;/system script run "GPS-Mode";} else={:if (($CompareLat < $LimitLat)&&($CompareLong < $LimitLong)) do={:log warning "Change To Mode2";:global Mode 2;/system script run "GPS-Mode";}};

Or there is a mistake in my logic?

Thanks ,