如何绑定到foreach上下文?

时间:2010-05-05 18:15:21

标签: rebol

目前我有这段代码:

    Blocks: ["F4369RO771" "282273" "5" "146" "126" "6-Nov-2009" "8-Jan-2010" "7-Jun-2010" "8"
"M9881KI923" "399727" "2" "359" "443" "5-Aug-2010" "23-Feb-2010" "6-Nov-2009" "4"
]

save-blocks: func[file /local f out][
    foreach [field1 field2 field3 field4 field5 field6 field7 field8 field9] blocks [

out: copy ""
repeat n 9 [
  part: get bind to-word rejoin ["field" n] 'field1 
  out: rejoin [out part ";"]
]
        remove back tail out
        write/lines/append f out


]

这不够通用,我想传递这个

block: [field1 field2 field3 field4 field5 field6 field7 field8 field9]

作为参数并写下这样的东西:

save-blocks: func[block file /local f out][
    foreach block blocks [

out: copy ""
repeat n length? block [
  part: get bind to-word rejoin ["field" n] 'field1 
  out: rejoin [out part ";"]
]
        remove back tail out
        write/lines/append f out


]

但我不知道如何在这种情况下对我来说太难了:(

1 个答案:

答案 0 :(得分:1)

(目前,无视你在这里做什么是个好主意。:P)

请记住,当您将block!传递给foreach时,它会在循环中绑定该块内的字词:

>> foreach [foo bar] ["a" "b"] [print foo print bar]
a
b

当您传递任何类型的word!时,它将被完全覆盖,并且不会分配任何变量(无论该字是否先前绑定到某个块!):

>> foobarblock: [foo bar]

>> foreach foobarblock ["a" "b"] [print foo print bar]
** Script error: foo has no value
** Where: foreach
** Near: foreach foobarblock ["a" "b"] [print foo print bar]

>> foreach foobarblock ["a" "b"] [print foobarblock]
a
b

为了获得你在这里想要的效果,你需要这样的东西:

>> foreach :foobarblock ["a" "b"] [print foo print bar]
a
b

这种行为将是您所期望的,并且在本地环境中绑定了单词。