[REBOL] Re: Multi-searches
From: carl:cybercraft at: 21-Feb-2002 19:56
On 21-Feb-02, Philippe Oehler wrote:
> Hi Joel,
> Yes Joel something like that. Thanks a lot ! I tried with the FIND
> word but you just did with a IF test. Sometimes the things are more,
> more simple as you expected 8-)
Hi Philippe,
Joel's answered you fine, but your question suggested another possible
approach to me if (though only if) you were looking for series within
a block. What the following does is to put what you're looking for
in the block that's returned instead of the index numbers. It's
easier to show you than explain I think, so...
find-series: func [blk [block!] ser [series!] /local result][
result: copy []
forall blk [
if blk/1 = ser [append result blk/1]
]
result
]
>> a: ["fe" "fi" "fo" "fe" "fi" "fo"]
== ["fe" "fi" "fo" "fe" "fi" "fo"]
>> b: find-series a "fo"
== ["fo" "fo"]
The two "fo"s in 'b there are the same strings that appear in 'a, so
that allows us to do the following kind of stuff...
>> append b/1 "1"
== "fo1"
>> a
== ["fe" "fi" "fo1" "fe" "fi" "fo"]
>> b
== ["fo1" "fo"]
and...
>> append b/2 "2"
== "fo2"
>> a
== ["fe" "fi" "fo1" "fe" "fi" "fo2"]
>> b
== ["fo1" "fo2"]
Hope that's of interest even if it's not of specific use to your
current project.
> Can you explain me something ? Why (result) is a local variable and
> then without the RETURN word, you typed (result) at the end of the
> function ? What is the advantage of this ?
> Philippe
>> Hi, Philippe,
>> Philippe Oehler wrote:
>>>
>>> I want to make multi-researches on a block of strings.
>>>
>>> The result I want is an array of index of all appereances of a
>>> name, but the 'find word find only the first appereance
>> Do you mean something like this?
>>>> blort: [1 2 0 3 0 4 5 6 0 7]
>> == [1 2 0 3 0 4 5 6 0 7]
>>>> find-all: func [b [block!] v [any-type!] /local result] [
>> [ result: copy []
>> [ forall b [if v = b/1 [append result index? b]]
>> [ result
>> [ ]
>>>> find-all blort 0
>> == [3 5 9]
>> I used a block of numbers to save typing, but the principle
>> is the same IMHO.
>> HTH!
>> -jn-
--
Carl Read