如何在REBOL多维数组中访问数组索引

时间:2014-04-08 18:10:09

标签: rebol

我尝试使用数组来指定二维数组的索引,但pick函数不接受数组作为第二个元素:

print pick [[3 5] [3 1]] [2 1]

*** ERROR
** Script error: invalid argument: [2 2]
** Where: pick try do either either either -apply-
** Near: pick [[3 5] [3 1]] [2 2]

我找到了一个解决方法,但它稍微冗长一点:

print pick pick [[3 5] [3 1]] 2 1
[comment This prints "3".]

是否可以多次调用pick函数来访问多维数组的索引?

1 个答案:

答案 0 :(得分:2)

从多维数组中PICK元素的更简洁方法是使用PATH!语法。

这是Rebol控制台中的一个示例:

>> x: [[3 5] [3 1]]     
== [[3 5] [3 1]]

>> x/2/1
== 3

>> x/2/2                      
== 1

>> x/1/(1 + 1)      ;; use parens for expressions - transforms to x/1/2
== 5

>> p: 2
== 2

>> x/1/:p           ;; use ":" for variable reference - transforms to x/1/2 
== 5

>> x/(p - 1)/:p     ;; mix and match at any level of array - transforms to x/1/2
== 5    

>> x/3              ;; NONE is returned if index does not exist
== none

>> x/2
== [3 1]

>> x/2/3            ;; again out of range
== none

另一种选择是FIRSTSECOND .. TENTH函数:

>> second first [[3 5] [3 1]] 
== 5

你甚至可以混合搭配:

>> x: [ [[1]] [[2]] [3 [4 5]] ]
== [[[1]] [[2]] [3 [4 5]]]

>> first pick x/3 2
== 4