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

\Based on C example:
include c:\cxpl\stdlib;     \provides StrCmp routine, etc.
int Haystack;               \('int' is used instead of 'char' for 2D array)
 
func Search(Str, First);    \Return first (or last) index for string in haystack
char Str; int First;
int I, SI;
[I:= 0;  SI:= 0;
repeat  if StrCmp(Str, Haystack(I)) = 0 then
                [if First then return I;
                SI:= I;     \save index
                ];
        I:= I+1;
until   Haystack(I) = 0;
return SI;
];
 
[Haystack:= ["Zig", "Zag", "Wally", "Ronald", "Bush",
             "Krusty", "Charlie", "Bush", "Boz", "Zag", 0];
Text(0, "Bush is at ");  IntOut(0, Search("Bush", true));  CrLf(0);
if Search("Washington", true) = 0 then
        Text(0, "Washington is not in the haystack^M^J");
Text(0, "First index for Zag: ");  IntOut(0, Search("Zag", true));  CrLf(0);
Text(0,  "Last index for Zag: ");  IntOut(0, Search("Zag", false)); CrLf(0);
]