rpra
March 31, 2014, 10:54am
1
I want to use a global assoc array in my script for storing some host->ip pairs, and I’ve encountered a problem:
when I first time define variable, it has type = “nothing”, and can’t be used as an array.
I have to check it’s type and push some value to it, and then I can use a construct like
:global IP;
:if ([:typeof $IP] = "nothing") do={:set $IP {""};}
:set ($IP->$hostname) $ipaddress
It could be much easier to explicitely define type during init.
Try first to use right syntax:
:global IP; → :global IP value=;
:if ([:typeof $IP] = “nothing”) do={:set $IP {“”};} → :if ([:typeof $IP] = “nothing”) do={ :set $IP value={“”} }
And now think about possibilities:
:global newEmptyArray value={“”};
or
:global newEmptyArray value=[:toarray “”];
A9691
July 26, 2016, 10:06am
3
The second solution is the right one: [:toarray “”] is a zero length array.
{“”} is an array with one element which is a zero length string.
Also, one can use
({})
to have an empty array literal.
I found out incorrect work for syntax :local Array1 ({})
Works correctly :local Array1 [:toarray “”]
:global fArrayTest do={
# correct work :local Array1 [:toarray ""]
# :local Array1 [:toarray ""]
# BUG, incorrect work :local Array1 ({})
:local Array1 ({})
:set ($Array1->([:len $Array1])) [:len $Array1]
:put "len(Array1): $[:len $Array1]"
:put "Array1:"
:put $Array1
}
Test sequence for :local Array1 ({})
[admin@MikroTik] > $fArrayTest;
len(Array1): 1
Array1:
0
[admin@MikroTik] > $fArrayTest;
len(Array1): 2
Array1:
0;1
[admin@MikroTik] > $fArrayTest;
len(Array1): 3
Array1:
0;1;2
Test sequence for :local Array1 [:toarray “”]
[admin@MikroTik] > $fArrayTest;
len(Array1): 1
Array1:
0
[admin@MikroTik] > $fArrayTest;
len(Array1): 1
Array1:
0
[admin@MikroTik] > $fArrayTest;
len(Array1): 1
Array1:
0