Community discussions

MikroTik App
 
rftnon
newbie
Topic Author
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

Remove variable in array / Remove item in array

Wed Mar 09, 2016 12:32 pm

Code :
:global aaa ([:toarray "jan,feb,mar,apr"]);

# i want remove variable "mar" in array aaa

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

???
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: i want remove variable in array

Wed Mar 09, 2016 12:45 pm

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"};
 
rftnon
newbie
Topic Author
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

Re: i want remove variable in array

Thu Mar 10, 2016 12:23 am

thank you boen_robot
Is there another solution for just remove a specific element ?
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4047
Joined: Wed May 11, 2011 6:08 pm

Re: i want remove variable in array

Thu Mar 10, 2016 12:30 am

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)
}
 
rftnon
newbie
Topic Author
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

Re: i want remove variable in array

Fri Mar 11, 2016 6:41 pm

Thank you ZeroByte

But this did not work Dear ... 8)
 
alli
newbie
Posts: 39
Joined: Tue Jan 24, 2017 5:43 pm

Re: i want remove variable in array

Wed Jun 13, 2018 4:17 am

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
 
Retral
newbie
Posts: 33
Joined: Wed Jul 25, 2018 9:10 pm

Re: i want remove variable in array

Sat Jun 13, 2020 7:09 pm

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
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3334
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: i want remove variable in array

Sat Jun 13, 2020 10:54 pm

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...
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: i want remove variable in array

Tue Aug 30, 2022 10:43 am

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 }}
Last edited by Railander on Tue Aug 30, 2022 11:14 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: i want remove variable in array

Tue Aug 30, 2022 11:05 am

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:
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: i want remove variable in array  [SOLVED]

Tue Aug 30, 2022 11:06 am

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
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 11:22 am

@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).
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 11:28 am

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
}
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 11:42 am

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)
Last edited by Railander on Tue Aug 30, 2022 12:10 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 11:49 am

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 11:54 am

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.
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:00 pm

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 ;
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:03 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:08 pm

… 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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:09 pm

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
    }
}
 
User avatar
Railander
Frequent Visitor
Frequent Visitor
Posts: 89
Joined: Thu Jun 16, 2016 11:30 pm

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:16 pm

oh wow, this actually works way better, thanks fam.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12442
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Remove variable in array / Remove item in array

Tue Aug 30, 2022 12:19 pm

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

Who is online

Users browsing this forum: No registered users and 12 guests