RosettaCodeData/Task/Search-a-list/XPL0/search-a-list.xpl0
Ingy döt Net d066446780 langs a-z
2013-04-10 22:43:41 -07:00

25 lines
924 B
Text

\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);
]