Community discussions

MikroTik App
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

What is the meaning of it?

Sat Nov 21, 2009 5:46 pm

The episode shown in picture_1 was extracted from "Scripting Mikrotik WiKi" about the usage of "find" command. I really want to know what is the exact meaning of the last figure "-1".Minus one? or dash one? I have try to "+3" or "-5", but result seems nothing change.
You do not have the required permissions to view the files attached to this post.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8716
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

Re: What is the meaning of it?

Sat Nov 21, 2009 6:14 pm

it's 'minus one'. 'find' will look for <arg2> starting at the position (<start> + 1) - not sure why... so,
[admin@MikroTik] > :put [:find "abc" "a" 0];   

[admin@MikroTik] > :put [:find "abc" "a" -1]; 
0
[admin@MikroTik] > 
Last edited by Chupaka on Sat Nov 21, 2009 6:17 pm, edited 1 time in total.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: What is the meaning of it?

Sat Nov 21, 2009 6:14 pm

It's the starting position, the string will be searched beyond that character position. Character positions are 0 based, so in the string "abc" the string "a" appears at position 0. Since the starting positions gives the character positions beyond which the string will be searched,
:put [:find "abc" "a" 0];
yields null, since there is no "a" beyond character position 0. You have to search from the very start of the string, which is character position -1 (minus one).
:put [:find "abc" "b" 0];
works fine, however, since "b" is beyond character position 0, as it is at character position 1.

Really that's not worth memorizing, since the starting position is an optional parameter that defaults to -1 and searches the entire string. If you do need to start searching further into the string, just remember that positions are given based on 0, most probably because the search simply treats the string as a 0 based array.
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

Re: What is the meaning of it?

Sun Nov 22, 2009 5:47 am

thanks