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

 
Module Checkit {
      Flush ' empty stack
      Inventory Queue Haystack=  "foo", "bar", "baz", "quux", "quuux", "quuuux", "bazola", "ztesch", "foo", "bar", "thud", "grunt"
      Append  Haystack, "foo", "bar", "bletch", "foo", "bar", "fum", "fred", "jim", "sheila", "barney", "flarp", "zxc"
      Append  Haystack,  "spqr", "wombat", "shme", "foo", "bar", "baz", "bongo", "spam", "eggs", "snork", "foo", "bar"
      Append  Haystack,  "zot", "blarg", "wibble", "toto", "titi", "tata", "tutu", "pippo", "pluto", "paperino", "aap"
      Append  Haystack,  "noot", "mies", "oogle", "foogle", "boogle", "zork", "gork", "bork"
      \\ Inventories are objects and we have access to properties using COM model 
      With HayStack, "index" as index
      Inventory Queue HayStackRev
      N=Each(HayStack, -1, 1)
      While N {
            Append HayStackRev, Eval$(N, N^)
      }
      With HayStackRev, "index" as indexRev
      Print Len(HayStack)
      Print Len(HayStackRev)
      local needle$
      \\ Print all elements using columns
      Print haystack
      Repeat {
                Input "Word to search for? (Leave blank to exit) ", needle$
                If needle$ <> "" Then {
                          If Exist(haystackrev,lcase$(needle$) ) Then {
                              Print "Found "; CHR$(34); needle$; CHR$(34); " at index "; STR$(len(haystackrev)-indexrev,"")
 
                              If Exist(haystack,lcase$(needle$) ) Then  {
                                    if len(haystackrev)-1<>indexrev+index then {
                                                Print "Found "; CHR$(34); needle$; CHR$(34); " at index "; STR$(Len(haystack)-index,"")
                                    }
                              }
                        } Else  Print CHR$(34); needle$; CHR$(34); " not found"    
            } Else Exit
      } Always     
}
CheckIt
 