红色语言的foreach中的变量值 [英] Variable value in foreach of Red language

查看:90
本文介绍了红色语言的foreach中的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过foreach循环向视图添加多个GUI元素:

I am using following code to add multiple GUI elements to a view via a foreach loop:

myRange: function [n][  ; to produce a vector of [1 2 3 4... n]
  vv: make vector! n 
  i: 1
  foreach j vv [
    vv/:i: i
    i: i + 1
    if i > n [break]]
  vv ]

view collect[ 
    foreach i myRange 10 [  
        print append "i in loop: " i
        keep [ t: text ]  keep append "message number: " i
        keep [field "entry"     button "Click" [t/text: "clicked"] return]
            ] ]

所有GUI元素都在生成中.但是代码append "message number: " i在所有文本元素中都显示i的值为12345678910,对于不同的文本元素,i的值为1、2、3 ... 10.

All GUI elements are being produced. But the code append "message number: " i is showing value of i to be 12345678910 in all text elements and not 1, 2, 3... 10 for different text elements.

此外,print append...语句正在产生以下输出:

Also, print append... statement is producing following output:

i in loop: 1
i in loop: 12
i in loop: 123
i in loop: 1234
i in loop: 12345
i in loop: 123456
i in loop: 1234567
i in loop: 12345678
i in loop: 123456789
i in loop: 12345678910

此外,单击任何按钮都仅更改最后添加的文本元素的文本.

Moreover, clicking any button changes text of only the last added text element.

问题出在哪里,如何解决?感谢您的帮助.

Where is the problem and how can it be solved? Thanks for your help.

看来该语言正在将我的代码从

It seems the language is converting my code from:

for i 1 10 1 [  
   print append "i in loop: " i  ]

收件人:

a_variable: "i in loop"
for i 1 10 1 [  
   print append a_variable i  ]

我和大多数用户想要的不是我想要的.在大多数语言中,字符串"i in loop"将被视为常量,并且不会转换为变量,因为用户未指定.恕我直言,如果不更改这些基本约定,其他语言的用户会更容易来到这里.

Which is not what I and (I think) most users want. In most languages a string "i in loop" will be taken as a constant and not converted to a variable since user has not specified it so. IMHO it will be easier for users of other languages to come here if such basic conventions are not altered.

推荐答案

每当看到类似这样的内容时,就意味着您无法创建新系列,而在重用现有系列.

Whenever you see something like this, it means you failed to create a new series and are reusing an existing series.

要解决这个问题,您需要使用copy

To get around that you need to create a new series with copy

例如.

print append copy "i in loop: " i

Rebol3/ren-c不再具有此问题,因为源代码是不可变的,因此您将收到包含此类代码的错误消息.

Rebol3/ren-c no longer has this problem because source code is immutable and so you will get an error message with such code.

这篇关于红色语言的foreach中的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆