Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

indexing - 尝试查找索引时处理“无”? 在Rebol中不存在的值?(Handling 'none' when trying to find the index? of a value that doesn't exist in Rebol?)

I have a block!

(我有block!) x: ["one" "two" "three"]

index? find x "two" index? find x "two" returns... 2

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

index? find x "four" index? find x "four" returns...

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

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

What's the best way to have index?

(拥有索引的最佳方法是什么?)

return none rather than the error?

(返回none而不是错误?)

  ask by Atomica translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
>> 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.

index?

(指数?)

expects a series.

(期待一系列。)

If the argument is none, it will raise an error.

(如果参数为none,则将引发错误。)

>>  find x "four"
== none

You can either check if the argument exists or is a series or guard against the error eg

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

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

or

(要么)

>> attempt [index? find x "four" none]
== none

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...