Task[edit]
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack).

Raise an exception if the needle is missing.

If there is more than one occurrence then return the smallest index to the needle.

Extra credit
Return the largest index to a needle that has multiple occurrences in the haystack.

See also
 Search a list of records

haystack$="apple orange pear cherry melon peach banana needle blueberry mango strawberry needle "
haystack$=haystack$+"pineapple grape kiwi blackberry plum raspberry needle cranberry apricot"
 
idx=1
do until word$(haystack$,idx)=""
idx=idx+1
loop
total=idx-1
 
needle$="needle"
'index of first occurrence
for i = 1 to total
    if word$(haystack$,i)=needle$ then exit for
next
print needle$;" first found at index ";i
 
'index of last occurrence
for j = total to 1
    if word$(haystack$,j)=needle$ then exit for
next
print needle$;" last found at index ";j
if i<>j then
    print "Multiple instances of ";needle$
    else
    print "Only one instance of ";needle$;" in list."
end if
 
'raise exception
needle$="cauliflower"
for k=1 to total
    if word$(haystack$,k)=needle$ then exit for
next
if k>total then
    print needle$;" not found in list."
else
    print needle$;" found at index ";k
end if