Subtraction of elements in the Mikrotik array (does not count the first element correctly)
Posted: Wed May 24, 2023 7:31 pm
I can't figure out how to write scripts in Mikrotik. Another problem. There is an array.
I'll even start from afar. You need to find the length of the phrase in the text. The phrase in the text is repeated several times, but the phrase itself is not constant, and changes slightly. Constant characters only at the beginning and end-of-line character at the end of a phrase. Therefore, I decided to find all occurrences of the end-of-line character (that is, \n). you can find the length in theory by subtracting the value of the previous odd occurrence from the value of the even occurrence. I immediately put the result of the entry into an array and already there I do the calculations. I do it like this:
In theory it should have been something like this:
Naturally, this is not the planned output itself as the result of the array, but an algorithm so that it is clear what is being subtracted.
The problem is that on the first pass, the array returns the correct results, that is, the ones after the equal sign. But then, no matter how much the script runs, it subtracts the value of position 39 from the value of position 1
Apparently this is due to the use of a global variable in the loop. But at the moment I could not think of another way to go through the cycle.
I'll even start from afar. You need to find the length of the phrase in the text. The phrase in the text is repeated several times, but the phrase itself is not constant, and changes slightly. Constant characters only at the beginning and end-of-line character at the end of a phrase. Therefore, I decided to find all occurrences of the end-of-line character (that is, \n). you can find the length in theory by subtracting the value of the previous odd occurrence from the value of the even occurrence. I immediately put the result of the entry into an array and already there I do the calculations. I do it like this:
Code: Select all
:local numbers "15,193,210,548,565,903,920,1258,1275,1613,1630,1968,1985,2323,2340,2668,2685,3013,3030,3290,3308,3588,3606,3934,3952,4290,4308,4646,4664,5002,5020,5348,5366,5694,5711,5915,5933,6261,6279,6507"
:local whereEnd [:toarray $numbers]
:foreach key,num in=$whereEnd do={
if (($key % 2)!=0) do={
:global previous $num
}
:global previous
if (($key % 2)=0) do={
:put ($num-$previous);
}
}
Code: Select all
Find position #0 at: 15 =15
Find position #1 at: 193
Find position #2 at: 210 210-193=17
Find position #3 at: 548
Find position #4 at: 565 565-548=17
Find position #5 at: 903
Find position #6 at: 920 920-903=17
Find position #7 at: 1258
Find position #8 at: 1275 1275-1258=17
Find position #9 at: 1613
Find position #10 at: 1630 1630-1613=17
Find position #11 at: 1968
Find position #12 at: 1985 1985-1968=17
Find position #13 at: 2323
Find position #14 at: 2340 2340-2323=17
Find position #15 at: 2668
Find position #16 at: 2685 2685-2668=17
Find position #17 at: 3013
Find position #18 at: 3030 3030-3013=17
Find position #19 at: 3290
Find position #20 at: 3308 3308-3290=18
Find position #21 at: 3588
Find position #22 at: 3606 3606-3588=18
Find position #23 at: 3934
Find position #24 at: 3952 3952-3934=18
Find position #25 at: 4290
Find position #26 at: 4308 4308-4290=18
Find position #27 at: 4646
Find position #28 at: 4664 4664-4646=18
Find position #29 at: 5002
Find position #30 at: 5020 5020-5002=18
Find position #31 at: 5348
Find position #32 at: 5366 5366-5348=18
Find position #33 at: 5694
Find position #34 at: 5711 5711-5694=17
Find position #35 at: 5915
Find position #36 at: 5933 5933-5915=18
Find position #37 at: 6261
Find position #38 at: 6279 6279-6261=18
Find position #39 at: 6507
The problem is that on the first pass, the array returns the correct results, that is, the ones after the equal sign. But then, no matter how much the script runs, it subtracts the value of position 39 from the value of position 1
Apparently this is due to the use of a global variable in the loop. But at the moment I could not think of another way to go through the cycle.