Page 1 of 1

Remove variable in array / Remove item in array

Posted: Wed Mar 09, 2016 12:32 pm
by rftnon
Code :
:global aaa ([:toarray "jan,feb,mar,apr"]);

# i want remove variable "mar" in array aaa

Output :
"aaa"={"jan,feb,apr"}

???

Re: i want remove variable in array

Posted: Wed Mar 09, 2016 12:45 pm
by boen_robot
Use the ":set" command, so:
:set aaa {"jan";"feb";"apr"};
Unfortunately, scripting doesn't allow you to just remove a specific element. You have to redefine the whole array, as above.

Speaking of which, your declaration can also become:
:global aaa {"jan";"feb";"mar";"apr"};

Re: i want remove variable in array

Posted: Thu Mar 10, 2016 12:23 am
by rftnon
thank you boen_robot
Is there another solution for just remove a specific element ?

Re: i want remove variable in array

Posted: Thu Mar 10, 2016 12:30 am
by ZeroByte
You could do it with a loop-

(pseudocode here since I'm not a whiz with Mikrotik's exact syntax)

:local index=index-of(value in $array)
while ( $index > 0 ) {
:local left=sub-array (0,$index, $array)
:local right = sub-array ($index+1,sizeof($array),$array)
$array=($left ; $right)
index=index-of(value in $array)
}

Re: i want remove variable in array

Posted: Fri Mar 11, 2016 6:41 pm
by rftnon
Thank you ZeroByte

But this did not work Dear ... 8)

Re: i want remove variable in array

Posted: Wed Jun 13, 2018 4:17 am
by alli
Calling :set on variable or array elements without any value seems to work like unset (v6.42.3), check this:
:global a {b=1}
/environment print
:set ($a->"b")
/environment print
:set $a
/environment print

Re: i want remove variable in array

Posted: Sat Jun 13, 2020 7:09 pm
by Retral
Calling :set on variable or array elements without any value seems to work like unset (v6.42.3), check this:
: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.
I think the place where others may have difficulty is when using arrays without defining the keys.
If you try this on arrays that have predefined numbered keys you cannot erase the key, just it's value.

For example:
: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

Re: i want remove variable in array

Posted: Sat Jun 13, 2020 10:54 pm
by Jotne
Delete "mar" from array "aaa"

Loop trough the array, send to new array all except "mar"
: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
There may by a better way to do this...

Re: i want remove variable in array

Posted: Tue Aug 30, 2022 10:43 am
by Railander
i was trying to do this and very frustrated that the scripting allows you to add elements to an array but not search and remove an element from an array.

after a lot of searching i found a workaround in converting the array to string and performing regexp on it.
: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 }}

Re: i want remove variable in array

Posted: Tue Aug 30, 2022 11:05 am
by rextended
That "workaround" is totally a mess and full of errors, some items are applied to fix other wrong items...

*******************
There may by a better way to do this...
You are right another time :lol:

Re: i want remove variable in array  [SOLVED]

Posted: Tue Aug 30, 2022 11:06 am
by rextended
The tilte of this topic is "i want remove variable in array" and I changed it to "Remove variable in array / Remove item in array" for better search and reference.

And this is exact procedure, as already wrote on one previous post

New example for remove one variable defined inside the array:
{
:local test {"a"=1;"b"=2;"c"=3}
:put $test
:set ($test->"b")
:put $test
}

For remove instead one ITEM insde the Array, is extremely simple, if you manage to understand...
{
: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
}

Obviously if you want remove all ITEMS than start, end, or containing some specific string, you must use a cycle, but for remove one exact item, the previous procedure is the fastest.

Added example on next post for remove more items againsta a RegEx / string

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 11:22 am
by Railander
@rextended
my example works so you must've seen it wrong. i had to add ; at the start and end of the array to use it as a delimiter and avoid unwanted matchers (10.0.0.1 matching 110.0.0.145, etc). but i'd love to see what is wrong in it.

in my case i had to match elements from one dynamic array against elements from another dynamic array, not just pick this one element i already know either the name or position in the array.

even seeing your examples i still find them lacking.
first one uses key,value, which requires matching the value and then acting on the key.
second one uses array length, which requires matching the position of the element and then acting on that position (also doesn't work on nested arrays).

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 11:28 am
by rextended
They are just examples, to build more elaborate functions.

If you don't give good examples, or detailed description, but just a generic mess, I (and the others) can't figure out what exactly you want...


For example, remove ITEM(s) within an array, matching RegEx or partial/full string
In this case, remove all elements of the array that contain "c"
{

: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
}

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 11:42 am
by Railander
my use-case was adding every interface to an interface-list, except if the interface was already present.
: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}}
way simpler if you ask me
(i just edited the variable names manually so there might be typos, but i can confirm it works as expected, since interface-lists will allow you to create duplicates and this script does not create duplicates)

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 11:49 am
by rextended
Restyling of the code, on this post viewtopic.php?p=954302#p954280 , without modifying the functionalities, for a better reading and comprehensibility:
: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
    }
}
Convert the first array to a string, unnecessarily adding ";" at the beginning and at the end,
then you use the elements of a second array as search words, but before using it add ";" with a "+" in front, but there is already ";" and the + is useless,
and then it doesn't do anything, except commenting...

Here I don't see any "solution" to delete an element inside an array.

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 11:54 am
by rextended
my use-case was adding every interface to an interface-list, except if the interface was already present.
One moment, leave me the time to read.

But just one question in the meantime.
Given that I wonder how they manage to change the number of interfaces, unless they are virtual,
it is no longer simple, in this specific case, to directly create a new list of interfaces by replacing the contents of the previous array, directly with the new one, already unique?

This consideration is based on how you described its use.

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:00 pm
by Railander
i like the shorter form of converting to string, thanks.
dunno why i added the + for the regexp matcher, at this point i was a long time at it and just trying the most inclusive stuff possible.

however, you do need to add the ; at the beginning and end of array to use as delimeters, otherwise you may 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 ;

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:03 pm
by Railander
i think your alternative solution to creating a separate list would cause problems for me as i'm using that interface list in ospf interface-templates, i think any change would cause all interfaces to flap. hence why i'm opting to do a check like this.

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:08 pm
by rextended
… 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 ;…
The correct search string is "(^|;)10.0.0.1(;|$)" = or have ; at start / end or is at the start / the end of the string

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:09 pm
by rextended
For me you use the array without any reason, this is the soluction ( on example list1 must be already exist )
Less code, no array.
/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
    }
}

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:16 pm
by Railander
oh wow, this actually works way better, thanks fam.

Re: Remove variable in array / Remove item in array

Posted: Tue Aug 30, 2022 12:19 pm
by rextended
I might sound grumpy because I don't know how to write English well,
but I always recommend that you state what you want to achieve at the end rather than focusing on one detail.
This is the famous XY problem