Community discussions

MikroTik App
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Can you control the length of the generated string? In ROS 6.XX.

Wed Jan 31, 2024 4:56 am

:local num 00 ;
for p from=0 to=99999 do={
/interface pppoe-client set pppoe-out1 comment=("SNA".$num+$p)
:delay 2
}
In a script like this, how can the content of the comment be changed to something like SNA00000, SNA00000 1, until SNA0099999. This way? Can't we just use addition directly, can we only concatenate characters?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Can you control the length of the generated string? In ROS 6.XX.

Wed Jan 31, 2024 3:21 pm

:local num 00 ;
is completely useless the ; and the 00 if not inside quotes, because numeric 000000000000000000000000000 is 0
(add "" is useless, if "00" is added to 1, first "00" is converted on a number, and is 0 + 1 = 1 // adding "1" as string to "00" is "001")

Your script, ignoring errors, change comment on same pppoe-client each 2 seconds on this sequence:
SNA1 / SNA2 / SNA3 .... SNA99997 / SNA99998 / SNA99999
It's not clear what it's for, you didn't explain it.

This generates consecutive labels, but does not read the already existing value, to modify it.

example code

{
    :local prefix    "SNA"
    :local numlength 7
    :local zeros     ""
    :local final     1
    :local temp      ""
    :for x from=0 to=($numlength - 1) do={:set final ($final * 10) ; :set zeros "0$zeros"}
    :for number from=0 to=($final - 1) do={
        :set temp "$zeros$number"
        :put ("SNA$[:pick $temp ([:len $temp] - $numlength) [:len $temp]]")
    }
}
If you can ignore the superfluos 00000, and the comment is already SNAxxxxxxxx, you can use it to increase of 1 the counter:

advanced code

/interface pppoe-client print where [set pppoe-out1 comment=("SNA$([:pick $comment 3 [:len $comment]] + 1)")]

This is what you want, if is to add 1 every time the script is launched.
If the comment do not exist or is invalid, the script set it to SNA0000001

example code

{
    :local iface     "pppoe-out1"
    :local prefix    "SNA"
    :local numlength 7
    :local zeros     ""
    :local final     1
    :local temp      ""
    :for x from=0 to=($numlength - 1) do={:set final ($final * 10) ; :set zeros "0$zeros"}

    /interface pppoe-client
    :local comment [get $iface comment]
    :local number  ([:tonum [:pick $comment [:len $prefix] [:len $comment]]] + 1)
    :set temp "$zeros$number"
    set $iface comment=("SNA$[:pick $temp ([:len $temp] - $numlength) [:len $temp]]")
}
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can you control the length of the generated string? In ROS 6.XX.

Sun Feb 04, 2024 5:20 am

Oh! Great God~~I may not have described it properly. The character prefix I want to get is SNA00/SNA01/SNA02... SNA09 and then concatenated with complete characters such as SNA000000-SNA009999/SNA100000-SNA0199999/SNA0900000-SNA0999999. The last 5 digits range from 00000 to 99999, with only 100000 range intervals.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: Can you control the length of the generated string? In ROS 6.XX.

Sun Feb 04, 2024 5:54 am

Inspired by you, 01-09 has been resolved! But there are still some issues with 00, SNA0000000-SNA0099999

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests