Page 1 of 1

Foreach syntax

Posted: Fri Dec 01, 2023 12:36 pm
by diamuxin
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.

Re: Foreach syntax

Posted: Fri Dec 01, 2023 1:50 pm
by diamuxin
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"
}
}


Re: Foreach syntax

Posted: Fri Dec 01, 2023 4:08 pm
by Amm0
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 ;)

Re: Foreach syntax

Posted: Fri Dec 01, 2023 4:40 pm
by diamuxin
OK, thanks for the tip @Amm0.

BR.

Re: Foreach syntax

Posted: Sat Dec 02, 2023 11:28 am
by rextended
I do not like that way.
Better this:
/interface list
add name=ListB
member set [find where list="ListA" and interface~"ospf"] list="ListB"

Re: Foreach syntax

Posted: Sat Dec 02, 2023 12:35 pm
by diamuxin
Good, thanks!