1、All usage scenarios of the move command (The solution is at the end)
1.1、move A to top
1.2、move A to bottom
1.3、move A before B
1.4、move A behind B
1.5、move A up one step
1.6、move A down one step
2、testing environment (/ip f m),all the entries set only comment(1-5)
2.1、add entries:
:for i from=1 to=5 do={/ip f m add chain=input action=log disabled=yes comment=$i}
2.2、set comment add the “nextid”,Easy to observe
:foreach i in=[/ip f m find ] do={/ip f m set $i comment=([/ip f m get $i comment]."-".$i)}
2.3、add commnet=A entry as source
/ip f m add chain=input action=log disabled=yes comment=A
2.4、check
/ip f m print
"[admin@R5] > /ip f m print
Flags: X - disabled, I - invalid, D - dynamic
0 X ;;; 1-*2C
chain=input action=log
1 X ;;; 2-*2D
chain=input action=log
2 X ;;; 3-*2E
chain=input action=log
3 X ;;; 4-*2F
chain=input action=log
4 X ;;; 5-*30
chain=input action=log
5 X ;;; A-*36
chain=input action=log "
3、The Key Points
3.1、nextid, The system will assign a unique ID to any new entry,Only with nextid can precise control be achieved
3.1.1、get the nextid by find
:put [/ip f m find comment~"^A"]
"[admin@R5] > :put [/ip f m find comment~"^A"]
*36"
3.1.2、get the nextid by array
:put [/ip f m find]
"[admin@R5] > :put [/ip f m find]
*2c;*2d;*2e;*2f;*30;*36"
3.2、Array operation
3.2.1、get the length of the array by “:len”
:put [:len [/ip f m find]]
"[admin@R5] > :put [:len [/ip f m find]]
6"
3.2.2、get nextid of the last one from the array by the command “:pick”
:put [:pick [/ip f m find] ([:len [/ip f m find]]-1) ]
"[admin@R5] > :put [:pick [/ip f m find] ([:len [/ip f m find]]-1) ]
*36"
3.2.3、get index(which is the position of an item in the queue) from array by “:find” , array index-number begin from 0
:put [:find [/ip f m find] [/ip f m find comment~"^A"] ]
"[admin@R5] > :put [:find [/ip f m find] [/ip f m find comment~"^A"] ]
5"
3.2.4、get the nextid from the array ,which before A,by the command “:pick”
:put [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^A"] ]-1) ]
"[admin@R5] > :put [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^A"] ]-1) ]
*30"
4、Wrong way
4.1、move( source,destination)
you cant use the index-number as the source and destination like this:
/ip f m move [:find [/ip f m find] [/ip f m find comment~"^A"] ] ([:find [/ip f m find] [/ip f m find comment~"^A"]]-1);
it just can move A up one step, if you run it continuously, it will move the wrong entry
Unless you run print first, the print command will update the index-number right
For example: Run this command continuously (A:5 B:4)
/ip f m move 5 4
the first time ,it can move A before B,the second time ,What you expected was to move B before A,but Actually, nothing happened
testing found , first time after you run /ip f m move 5 4 , the index-number still be (A:5 B:4) ,this is the reason
and then run /ip f m print ,the index-number will be update (A:4 B:5)
Solution:
1、All usage scenarios of the move command
IF source is multiple,The script cannot determine the order of multiple entries reaching a new location
IF destination is multiple,The script cannot determine the the location of insert
Therefore, it is recommended to make a globally unique comment for each entry
In this experiment, B can be replaced with the numbers 1-5
1.1、move A to top (A can be unique or multiple)
/ip f m move [find comment~"^A"] 0;
1.2、move A to bottom (A can only be unique)
Two sentences
first: move A to the Penultimate
second: move the last one before A
/ip f m move [find comment~"^A"] [:pick [/ip f m find] ([:len [/ip f m find]]-1) ];
/ip f m move [:pick [/ip f m find] ([:len [/ip f m find]]-1) ] [find comment~"^A"];
1.3、move A before B (A can be unique or multiple,B can only be unique)
/ip f m move [/ip f m find comment~"^A"] [/ip f m find comment~"^B"];
1.4、move A behind B (A can only be unique,B can only be unique)
1.4.1、Two sentences (recommended)
move A before B,and move B before A
/ip f m move [/ip f m find comment~"^A"] [/ip f m find comment~"^B"];
/ip f m move [/ip f m find comment~"^B"] [/ip f m find comment~"^A"];
1.4.2、Two sentences
move A before B,and move A down one step
/ip f m move [/ip f m find comment~"^A"] [/ip f m find comment~"^B"];
/ip f m move [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^A"] ]+1) ] [/ip f m find comment~"^A"];
1.4.3、One sentences (V6:Failed(there is a bug when find different comment at multiple location in one sentences),v7:Successful)
move A before C which one is behind B
(but you must sure the B not be the last one,if B is the last one you still need to use the first method, so let’s just use the first method 1.4.1)
/ip f m move [/ip f m find comment~"^A"] [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^B"]]+1)];
1.5、move A up one step (A can only be unique)
/ip f m move [/ip f m find comment~"^A"] [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^A"] ]-1) ];
1.6、move A down one step (A can only be unique)
/ip f m move [:pick [/ip f m find] ([:find [/ip f m find] [/ip f m find comment~"^A"] ]+1) ] [/ip f m find comment~"^A"];
I hope it can be helpful to everyone. If you have a better method, please help to supplement it.