如何绑定到foreach上下文?

发布于 2024-08-31 23:22:10 字数 981 浏览 1 评论 0原文

目前我有这段代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-09-07 23:22:10

(目前,忽略您在这里所做的是否是个好主意的问题。: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

然后,行为将是您所期望的,单词绑定在本地上下文中。

(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! to foreach, it will bind the words inside that block during the loop:

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

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!):

>> 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

To get the effect you seem to be desiring here, you need something like:

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

The behavior would then be what you expect, with the words bound in the local context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文