Code :
:global aaa ([:toarray "jan,feb,mar,apr"]);
# i want remove variable "mar" in array aaa
Output :
"aaa"={"jan,feb,apr"}
???
:set aaa {"jan";"feb";"apr"};
:global aaa {"jan";"feb";"mar";"apr"};
:global a {b=1}
/environment print
:set ($a->"b")
/environment print
:set $a
/environment print
Thank you very much for this sir. You are correct.Calling :set on variable or array elements without any value seems to work like unset (v6.42.3), check this:
Code: Select all:global a {b=1} /environment print :set ($a->"b") /environment print :set $a /environment print
:global a {"val1";"val2";"val3"} <- I realize there are no keys identified here, but RouterOS sets those values keys as numbered starting at 0. Sometimes this is easier for certain things I do.
/environment print
:set ($a->0) <- This erases the keys value but not the key, whereas your method deletes it by the key, which is exactly what I needed last night, so again thank you ;)
/environment print
:set $a
/environment print
:local delete "mar"
:local array [:toarray ""]
:local aaa ([:toarray "jan,feb,mar,apr"])
:foreach i in=$aaa do={
:if ( $i!=$delete ) do={
:set array ( $array, $i )
}
}
:set aaa $array
:put $aaa
:local array1String (";".[:tostr $array1].";")
:foreach arrayElement in=[$array2] do={
:if ($arrayString~";+$arrayElement;+") do={ action to be performed on matchers here }\
else={ action to be performed on non-matchers here }}
You are right another timeThere may by a better way to do this...
{
:local test {"a"=1;"b"=2;"c"=3}
:put $test
:set ($test->"b")
:put $test
}
{
:local test {"bb";"dddd";"a";"ccc"}
:put $test
:local arrlen [:len $test]
:local arrpos [:find $test "dddd" -1]
:set test ([:pick $test 0 $arrpos],[:pick $test ($arrpos + 1) $arrlen])
:put $test
}
{
:local test {"bb";"dddd";"a";"ccc";"abcd";"befgh"}
:local search "c" ; # can be a simple string or a RegEx
:put $test
:local arrlen [:len $test]
:local arrpos
:foreach item in=$test do={
:if ($item ~ $search) do={
:set arrpos [:find $test $item -1]
:set test ([:pick $test 0 $arrpos],[:pick $test ($arrpos + 1) $arrlen])
}
}
:if ([:typeof $arrpos] = "nothing") do={ :put "Nothing replaced, $search not found" }
:put $test
}
:local intf [:toarray ""]
:foreach i in=[/intferface/find] do={
:set intf ($intf,[/intferface/get $i name])}
:local list [:toarray ""]
:foreach l in=[/intferface/list/member/find where list=list1] do={
:set list ($list,[/intferface/list/member/get $l intferface])}
:local listStr (";".[:tostr $list].";")
:foreach i in=[$intf] do={
:if ($listStr~";+$i;+") do={}\
else={/intferface/list/member/add list=list1 intferface=$i disabled=yes}}
:local array1String ";$[:tostr $array1];"
:foreach arrayElement in=$array2 do={
:if ($arrayString~";+$arrayElement;+") do={
# action to be performed on matchers here
} else={
# action to be performed on non-matchers here
}
}
One moment, leave me the time to read.my use-case was adding every interface to an interface-list, except if the interface was already present.
The correct search string is "(^|;)10.0.0.1(;|$)" = or have ; at start / end or is at the start / the end of the string… search for 10.0.0.1 and match 10.0.0.123 in {1;2;3;10.0.0.123}, and if you search for ;10.0.0.1; you won't find anything because your match is at the end of the array with no trailing ;…
/interface
:foreach i in=[find] do={
:local iname [get $i name]
:if ([:len [list member find where list=list1 and interface=$iname]] = 0) do={
list member add list=list1 interface=$iname disabled=yes
}
}