Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
38
Task/Search-a-list/REBOL/search-a-list.rebol
Normal file
38
Task/Search-a-list/REBOL/search-a-list.rebol
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
REBOL [
|
||||
Title: "List Indexing"
|
||||
URL: http://rosettacode.org/wiki/Index_in_a_list
|
||||
]
|
||||
|
||||
locate: func [
|
||||
"Find the index of a string (needle) in string collection (haystack)."
|
||||
haystack [series!] "List of values to search."
|
||||
needle [string!] "String to find in value list."
|
||||
/largest "Return the largest index if more than one needle."
|
||||
/local i
|
||||
][
|
||||
i: either largest [
|
||||
find/reverse tail haystack needle][find haystack needle]
|
||||
either i [return index? i][
|
||||
throw reform [needle "is not in haystack."]
|
||||
]
|
||||
]
|
||||
|
||||
; Note that REBOL uses 1-base lists instead of 0-based like most
|
||||
; computer languages. Therefore, the index provided will be one
|
||||
; higher than other results on this page.
|
||||
|
||||
haystack: parse "Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo" none
|
||||
|
||||
print "Search for first occurance:"
|
||||
foreach needle ["Washington" "Bush"] [
|
||||
print catch [
|
||||
reform [needle "=>" locate haystack needle]
|
||||
]
|
||||
]
|
||||
|
||||
print [crlf "Search for last occurance:"]
|
||||
foreach needle ["Washington" "Bush"] [
|
||||
print catch [
|
||||
reform [needle "=>" locate/largest haystack needle]
|
||||
]
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue