index? none
[1/6] from: tedlandis:rogers at: 28-Nov-2002 10:45
Hi
How do I best avoid the error possible in the following:
index? find block x
When x is not in block it returns none and index? returns an error.
Thanks,
Ted
[2/6] from: mike:yaunish:shaw:ca at: 28-Nov-2002 10:56
Try:
if ( found? f: find block x ) [ index? f ]
[3/6] from: joel:neely:fedex at: 28-Nov-2002 11:55
Hi, Ted,
Ted Landis wrote:
> How do I best avoid the error possible in the following:
>
> index? find block x
>
> When x is not in block it returns none and index? returns an error.
>
It depends on what you want to do ...
a) with the index value
b) when the target value isn't found
... but here's one possibility ...
either found? here: find block x [
... index? here ... ;; handle located value
][
... ;; handle the not-found case
]
... and another ...
index? any [find block x tail block]
(e.g. when you want to append not-found values to the block)
HTH!
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]
[4/6] from: g:santilli:tiscalinet:it at: 28-Nov-2002 19:47
Hi Ted,
On Thursday, November 28, 2002, 4:45:34 PM, you wrote:
TL> How do I best avoid the error possible in the following:
TL> index? find block x
TL> When x is not in block it returns none and index? returns an error.
It depends on what you want it to return in that case... anyway,
you can use:
index? any [find block x #]
(I think I saw the issue trick from Jeff the first time.)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[5/6] from: tedlandis::rogers::com at: 28-Nov-2002 13:38
Excellent Joel!
The "any" solution fits best in my real case but I like the other one
too. I ended up hacking in this while I was waiting for feedback:
if error? try [z: index? find block x] [z: "Not there"]
but that's probably overkill... I need to remember that "any" is not
just for conditional expressions!
Thanks again!
Ted
On Thursday, Nov 28, 2002, at 12:55 Canada/Eastern, Joel Neely wrote:
[6/6] from: carl:cybercraft at: 29-Nov-2002 9:16
On 29-Nov-02, Ted Landis wrote:
> Hi
> How do I best avoid the error possible in the following:
> index? find block x
> When x is not in block it returns none and index? returns an error.
Hi Ted,
Here's one way...
>> block: ["a" "b" "c"]
== ["a" "b" "c"]
>> if block? blk: find block "b" [index? blk]
== 2
>> if block? blk: find block "d" [index? blk]
== none
or if you want a number returned instead of a none you could use...
>> either block? blk: find block "d" [index? blk][0]
== 0
Hope that helps.
--
Carl Read