I’m at RouterOS 6.35.1 and not sure if I should upgrade to 6.35.2 given the number of bugs mentioned there.
There seems to be something odd with :pick parameters on strings.
From the from the documentation:
- Command pick
- Syntax :pick []
- Description return range of elements or substring. If end position is not specified, will return only one element from an array.
- Example :put [:pick “abcde” 1 3]
I’d expect both the start and end parameters to be both based on either the first character of a string at position zero or at position one.
It looks however that the start parameter assumes the first character to be at position zero, but the example from the documentation returns only two characters where I expected three:
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1 3]
bc
What am I missing here?
To elaborate on my suspicion, I created more test cases.
Only a start parameter: it indicates position zero is the start of the string and four the end of the string. Position five is beyond the end of the string.
[admin@MikroTikCCR1009] > :put [:pick "abcde" 0]
a
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1]
b
[admin@MikroTikCCR1009] > :put [:pick "abcde" 2]
c
[admin@MikroTikCCR1009] > :put [:pick "abcde" 3]
d
[admin@MikroTikCCR1009] > :put [:pick "abcde" 4]
e
[admin@MikroTikCCR1009] > :put [:pick "abcde" 5]
The end parameter however behaves strangely as all these should have returned a string with one character:
[admin@MikroTikCCR1009] > :put [:pick "abcde" 0 0]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1 1]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 2 2]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 3 3]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 4 4]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 5 5]
Being a programmer by trade, I guessed an off-by one error and that indeed seems to be the case, as all these return exactly one letter except for the last one (as five is beyond the string when using zero based indexing and six is beyond the string for one based indexing).
[admin@MikroTikCCR1009] > :put [:pick "abcde" 0 1]
a
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1 2]
b
[admin@MikroTikCCR1009] > :put [:pick "abcde" 2 3]
c
[admin@MikroTikCCR1009] > :put [:pick "abcde" 3 4]
d
[admin@MikroTikCCR1009] > :put [:pick "abcde" 4 5]
e
[admin@MikroTikCCR1009] > :put [:pick "abcde" 5 6]
This seems to cofurm that end is indeed the finishing position (at d the wrong one, but at least they are consistently using d):
[admin@MikroTikCCR1009] > :put [:pick "abcde" 0 4]
abcd
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1 4]
bcd
[admin@MikroTikCCR1009] > :put [:pick "abcde" 2 4]
cd
[admin@MikroTikCCR1009] > :put [:pick "abcde" 3 4]
d
[admin@MikroTikCCR1009] > :put [:pick "abcde" 4 4]
[admin@MikroTikCCR1009] > :put [:pick "abcde" 5 4]
But using five instead of four gives the expected strings ending with character e:
[admin@MikroTikCCR1009] > :put [:pick "abcde" 0 5]
abcde
[admin@MikroTikCCR1009] > :put [:pick "abcde" 1 5]
bcde
[admin@MikroTikCCR1009] > :put [:pick "abcde" 2 5]
cde
[admin@MikroTikCCR1009] > :put [:pick "abcde" 3 5]
de
[admin@MikroTikCCR1009] > :put [:pick "abcde" 4 5]
e
[admin@MikroTikCCR1009] > :put [:pick "abcde" 5 5]
–jeroen