i would like to use the “in” function which works fine for the “lightly” documented type ip-prefix. But i have a
strings that looks like “1.2.3.4/3” how could i convert this string to a ip-prefix type so that
this will work?
/ip route> :global a "1.2.3.4/4"
/ip route> :global b "1.2.3.4/4"
/ip route> :put ($a in $b)
false
/ip route> :global a [/ip route get 1 dst-address]
/ip route> :global b [/ip route get 1 dst-address]
/ip route> :put ($a in $b)
true
I’m not certain, but I think the first example you gave is not valid, so it will always be false.
Initially, I thought using :toip would work, but apparently not with the mask. The string is converted to a blank variable with a type of “nil”
:global a “192.168.0.10/32”
:global b “192.168.0.0/24”
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: str, b: str
:set a [:toip $a]
:set b [:toip $b]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: nil, b: nil
Funny that if you do it this way, the variable type is “nothing”:
:global a [:toip “192.168.0.10/32”]
:global b [:toip “192.168.0.0/24”]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: nothing, b: nothing
The :toip conversion works fine without the mask, but the mask is necessary for the “in” function to work:
:global a “192.168.0.10”
:global b “192.168.0.0”
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: str, b: str
:set a [:toip $a]
:set b [:toip $b]
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: ip, b: ip
:put ($a in $b)
false
Remove the quotes and it works normally:
:global a 192.168.0.10/32
:global b 192.168.0.0/24
:put “a: $[:typeof $a], b: $[:typeof $b]”
a: ip-prefix, b: ip-prefix
:put ($a in $b)
true