我想要
[["1" "a"] ["2" "b"]]
来自
["1" "" "a" "2" "" "b"]
什么是正确和最优雅的方式?我尝试了这个,但它没有得到我想要的结果:
block: ["1" "" "a" "2" "" "b"]
new-block: []
sub-block: []
foreach [first second third] block [
sub-block: []
append sub-block first
append sub-block third
append new-block sub-block
]
答案 0 :(得分:4)
newblock: copy [] forskip block 3 [repend/only newblock [block/1 block/3 ]]
== [["1" "a"] ["2" "b"]]
答案 1 :(得分:4)
foreach [a b c] block [repend/only [] [a c]]
答案 2 :(得分:2)
另一种方法是使用COLLECT/KEEP
:
collect [
foreach [first second third] block [
keep/only collect [
keep first
keep third
]
]
]
答案 3 :(得分:1)
另一种不太优雅的方法是:
>> parse ["1" "" "a" "2" "" "b"] [
2 [set a string! skip set b string!
(append x: [] reduce [a b])
]
]
>> x
== ["1" "a" "2" "b"]
答案 4 :(得分:1)
对于更多种类,这是使用MAP-EACH
map-each [first second third] block [reduce [first third]]
NB。以上在Rebol(2& 3)中有效。 MAP-EACH
目前在Red中不可用。