我尝试使用数组来指定二维数组的索引,但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
函数来访问多维数组的索引?
答案 0 :(得分:2)
这是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
另一种选择是FIRST
,SECOND
.. 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