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

DEFINE PTR="CARD"
 
INT FUNC Search(PTR ARRAY texts INT count CHAR ARRAY text)
  INT i
 
  FOR i=0 TO count-1
  DO
    IF SCompare(texts(i),text)=0 THEN
      RETURN (i)
    FI
  OD
RETURN (-1)
 
PROC Test(PTR ARRAY texts INT count CHAR ARRAY text)
  INT index
 
  index=Search(texts,count,text)
  IF index=-1 THEN
    PrintF("""%S"" is not in haystack.%E",text)
  ELSE
    PrintF("""%S"" is on index %I in haystack.%E",text,index)
  FI
RETURN
 
PROC Main()
  PTR ARRAY texts(7)
 
  texts(0)="Monday"
  texts(1)="Tuesday"
  texts(2)="Wednesday"
  texts(3)="Thursday"
  texts(4)="Friday"
  texts(5)="Saturday"
  texts(6)="Sunday"
 
  Test(texts,7,"Monday")
  Test(texts,7,"Sunday")
  Test(texts,7,"Thursday")
  Test(texts,7,"Weekend")
RETURN