试图查找索引时处理'none'? Rebol中不存在的值的整数?

问题描述 投票:0回答:1

我有一个block! x: ["one" "two" "three"]

index? find x "two"返回... 2

index? find x "four"返回...

** Script Error: index? expected series argument of type: series port ** Near: index? find x "twos"

拥有索引的最佳方法是什么?返回none,而不是错误?

indexing rebol rebol2
1个回答
0
投票
>> help index?
USAGE:
    INDEX? series /xy

DESCRIPTION:
     Returns the index number of the current position in the series.
     INDEX? is an action value.

ARGUMENTS:
     series -- (Type: series port)

REFINEMENTS:
     /xy -- Returns index as an XY pair offset.

索引?期待一系列。如果参数为none,则将引发错误。

>>  find x "four"
== none

您可以检查参数是否存在或为序列或防止错误,例如

>> if  series? i: find x "four" [index? i]
== none

>> attempt [index? find x "four" none]
== none
© www.soinside.com 2019 - 2024. All rights reserved.