#22 Find word number given a string index

open
5
2006-03-25
2006-03-25
Anonymous
No

I would like to submit a new function with regard to
string handling: given an index into a string, what
word number am I in? This is the converse to function
wordindex. I call it posword, and here is the code I
came up with:

posword: procedure
/* For a given string index, what word number am I in?
Contrast wordindex. */
/* Returns zero if the index is not numeric or not
within the input length. */
index = arg(1)
mystr = arg(2)
if \datatype(index, 'Numeric') then return 0
if index<1 | index>length(mystr) then return 0
if strip(mystr) = '' then return 0

curpos = 0
do i = 1 to words(mystr) until curpos >= index
curpos = wordindex(mystr, i)
end

if curpos=index then return i
else return i-1

-------------------

Thanks and regards,
Chris Niggeler
cniggeler@yahoo.com

Discussion