misinterpreting searchindex


#1

Perhaps I am misinterpreting how searchindex() or search() works.
The docs say, “The third argument optionally specifies a starting index.”

Let’s see what I get:

searchindex("Hello, World!", "o")
5
searchindex("Hello, World!", "o", 6)
9

I originally thought that because, in the 2nd case, the search starts at character 6 that the resulting index would be 3. But, that is not the case: the index is always calculated from the beginning of the 1st string argument–only the comparison begins at the position of the 3rd argument.

It works how it works and I can achieve what I need. It would have made more sense, to me at least, if the index was calculated from where the comparison started.


#2

I think that would make it hard to work with, because then to index into the string, you’d always have keep the index where the comparison started, and add it to the result to get an index that could be used to index the string.


#3

You are completely right.

I have switched MY interpretation to the way it really works–it is more useful.