[REBOL] Re: findany
From: carl:cybercraft at: 24-Dec-2001 18:51
On 24-Dec-01, Dr. Louis A. Turk wrote:
> Rebol friends,
> Why won't the following function work? Is there a better way to do
> this? Is there a native rebol function for this that I have missed?
> Louis
> findany: func [
> "Searches string x for any substring found in block ys."
> x [string!] "string"
> ys [block!] "block of substrings"
> ] [
> foreach y ys [
> if find x y [true break]
> ]
> ]
> x: "There is a dog in the barn."
> ys: ["cat" "pig" "dog" "skunk"]
> if findany x ys [print "found"]
Instead of the [true break], use [return true].
However, it might be better to write it so what's returned is the
position in the string where the match is made, instead of just true.
ie...
findany: func [
"Searches string x for any substring found in block ys."
x [string!] "string"
ys [block!] "block of substrings"
/local pos
] [
foreach y ys [
if pos: find x y [return pos]
]
]
x: "There is a dog in the barn."
ys: ["cat" "pig" "dog" "skunk"]
print findany x ys
That will return the string at where the match was found, or a none if
no match was found.
Oh yes, and Merry Christmas too, Louis. (:
--
Carl Read