如何绑定到foreach上下文?
目前我有这段代码:
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
]
但我不知道如何在这种情况下绑定对我来说太难了:(
Currently I have this snippet of code:
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
]
It's not generic enough, I'd like to pass this instead
block: [field1 field2 field3 field4 field5 field6 field7 field8 field9]
as parameter and write something like this:
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
]
But I don't know how to bind in this case too hard for me :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(目前,忽略您在这里所做的是否是个好主意的问题。:P)
请记住,当您将
block!
传递给时foreach
,它会在循环期间绑定该块内的单词:当您传递任何类型的
word!
时,它将被完全覆盖,并且不会分配任何变量(无论是否该单词之前绑定到一个块!):为了获得您想要的效果,您需要类似的内容:
然后,行为将是您所期望的,单词绑定在本地上下文中。
(For the moment, disregarding the question of whether what you're doing here is a good idea. :P)
Remember that when you pass a
block!
toforeach
, it will bind the words inside that block during the loop:When you pass a
word!
of any kind, it will be overwritten completely and no variables will be assigned (regardless of whether that word was bound to previously to a block!):To get the effect you seem to be desiring here, you need something like:
The behavior would then be what you expect, with the words bound in the local context.