Community discussions

MikroTik App
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Foreach syntax

Fri Dec 01, 2023 12:36 pm

Hi, could you help me with this?
It may be silly but I don't know what I'm missing...

I want to change the "interface list" of a number of interfaces that contain the word "ospf" and replace ListA with the new ListB.
[ ] > interface/list/member pr
Columns: LIST, INTERFACE
# LIST      INTERFACE
0 ListA     wg-ospf-to-A
1 ListA     wg-ospf-to-B
2 ListA     wg-ospf-to-C
3 ListA     wg-ospf-to-D

{
/interface list
add name=ListB

:delay 1s

/interface
:foreach i in=[find name~"ospf"] do={
    list/member set $i list=ListB
}
}

Output: 
no such item (4)
In Winbox it is easy to change it, but I want to learn how to do it by CLI.

Thanks in advance.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Foreach syntax

Fri Dec 01, 2023 1:50 pm

Sorry, but after several tests I have already discovered the solution.

{
/interface list
add name=ListB

:delay 1s

/interface/list/member
:foreach i in=[find where ((interface~"ospf") and (list~"ListA"))] do={
    set $i list="ListB"
}
}

 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3542
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Foreach syntax

Fri Dec 01, 2023 4:08 pm

Sorry, but after several tests I have already discovered the solution.
No apologizes needed for solving your own problem :)

One tip to help spotting the inheiret command path is using spaces & curly braces after the path and put your code there. Just style, but helps keep track visually what the [find] refers.

/interface/list {
   add name=ListB
   :delay 1s
}

/interface/list/member {
  :foreach i in=[find where ((interface~"ospf") and (list~"ListA"))] do={
      set $i list="ListB"
  }
}

Both work, just an idea. But you might have spotted the /interface vs /interface/list/member sooner ;)
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Foreach syntax

Fri Dec 01, 2023 4:40 pm

OK, thanks for the tip @Amm0.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12024
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Foreach syntax

Sat Dec 02, 2023 11:28 am

I do not like that way.
Better this:
/interface list
add name=ListB
member set [find where list="ListA" and interface~"ospf"] list="ListB"
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Foreach syntax

Sat Dec 02, 2023 12:35 pm

Good, thanks!

Who is online

Users browsing this forum: No registered users and 5 guests