简体   繁体   English

我需要插入计数的 output,从 TCL 中可变“列表”中的循环“for”递增

[英]I need to insert the output of the count, incremented from a loop 'for' within a varyable 'list' in TCL

I want to list the increments in "$ i" of the loop for variable in '[list ]' mode.我想在“[list]”模式下列出变量循环的“$ i”中的增量。 For then, I could calculate the values with '[expr ]'.那时,我可以用“[expr]”计算值。

I will leave here a didactic example, to simplify the idea.我将在这里留下一个教学示例,以简化这个想法。 To look:看:

set i 1

while {$i <= 9} {
  global value
  set lst [puts -nonewline "$i "]
  set value [list $lst]
  incr i
}

global value
# puts [expr [lindex $value 0] + [lindex $value 1]]
puts "\n$value"

Result结果在此处输入图像描述


My difficulty is in keeping these successive increments within a variable.我的困难是将这些连续的增量保持在一个变量内。

In this mode:在这种模式下:

set value $i

Only the last increment / number is added in the variable.只有最后一个增量/数字被添加到变量中。

As I said earlier, I need to store these numbers numbers to later calculate.正如我之前所说,我需要存储这些数字以便以后计算。 Something like:就像是:

puts [expr [lindex $value 0] + [lindex $value 1]]

Warning - "For didactic purposes here, I made an example with ordinal number and sequence of '1 - 9'. But for my purpose that I am working at the moment.. I am receiving random numbers from one or more decimal places, I leave it very clear to the colleagues who can help do not suggest writing the list by hand. Once I am extracting lines from a file that contains 1000 lines from which it is numbered, that means that, I am working with dynamic and non-sequential number as it is in the question. I only made '1-9' to be able to exemplify the issue. After receiving the answer myself I will adapt the solution to my script.tcl."警告- “为了教学目的,我用序数和序列 '1 - 9' 做了一个例子。但为了我目前正在工作的目的..我收到一个或多个小数位的随机数,我让可以帮助的同事非常清楚,不要建议手动编写列表。一旦我从一个包含 1000 行编号的文件中提取行,这意味着,我正在使用动态和非顺序问题中的数字。我只做了'1-9'来举例说明这个问题。我自己收到答案后,我会根据我的script.tcl调整解决方案。“

I made this a little more interesting by adding a random value to i instead of incrementing by one.我通过向i添加一个随机值而不是加一来使这变得更有趣。

set prev 0
set i 1
set i_values {}
set increments {}
while {$i <= 9} {
    lappend i_values $i
    lappend increments [expr {$i - $prev}]
    set prev $i
    set i [expr {$i + rand()}]
}
puts $i_values
puts $increments
1 1.5047644425671383 2.08075066845899 2.681249232814298 3.2606203524678112 3.751028369064922 4.0385633167059 4.638428318611546 5.569515346814653 6.3491983564334 6.481541018691633 6.764665592817901 7.239382933005405 7.8137194643792345 8.687802264321506
1 0.5047644425671383 0.5759862258918518 0.6004985643553078 0.5793711196535134 0.49040801659711075 0.28753494764097765 0.5998650019056466 0.9310870282031072 0.7796830096187461 0.13234266225823355 0.28312457412626824 0.474717340187504 0.5743365313738291 0.874082799942272

You've got two options really for incrementally building a list.您实际上有两个选项可用于逐步构建列表。 The first is to use lappend , and the second (with 8.6 onwards) is to use lset with the index end+1 .第一个是使用lappend ,第二个(从 8.6 开始)是使用lset和索引end+1 Or you can build a list of the right length up front with lrepeat and then lset the values into it.或者,您可以使用lrepeat lset一个正确长度的列表,然后将值设置到其中。

Tcl's indices are always either zero-based or end based. Tcl 的索引总是基于零或基于end

Here's an example where each element of the generated list is 1 greater than the sum of the previous two values.这是一个示例,其中生成的列表的每个元素都比前两个值的总和大 1。 You need to initialise the list with two seed values.您需要使用两个种子值初始化列表。

set myList {0 1}
for {set i 0} {$i < 10} {incr i} {
    set v1 [lindex $myList $i]
    set v2 [lindex $myList [expr {$i + 1}]]
    lappend myList [expr {$v1 + $v2 + 1}]
}
puts $myList
# 0 1 2 4 7 12 20 33 54 88 143 232

Note that you can't use foreach to do this;请注意,您不能使用foreach来执行此操作; that always iterates over the eemphasized textlements of a fixed (at the time of calling) list value.它总是迭代固定(在调用时)列表值的强调文本。

Glenn Jackman answer shows the usage of lappend to append an element to a list . Glenn Jackman的回答显示了将lappend用于 append 的元素到list的用法。

Chris Heithoff clarified to me in his comment that, it is a common mistake with lappend to use the $ dollar sign in front of the variable list name. Chris Heithoff在他的评论中向我澄清说,在变量列表名称前使用 $ 美元符号是lappend的一个常见错误。

Donal Fellows warned me before I could even fall into the temptation to do this with foreach -, Note that you can't use foreach to do this; Donal Fellows在我陷入使用foreach执行此操作的诱惑之前就警告过我 -请注意,您不能使用foreach执行此操作; that always iterates over the eemphasized textlements of a fixed (at the time of calling) list value.它总是迭代固定(在调用时)列表值的强调文本。

#!/bin/ash
# \
exec tclsh "$0" "$@"

# list
set value {}
set i 1
while {$i <= 9} {

    # Add the stdout in "$i" for list       
        lappend value $i
    
        incr i
}

# Note that in this calculation, the left side is placed the even numbers and the Right are the Odd ones.

puts [expr [lindex $value 0] + [lindex $value 1]]

puts [expr [lindex $value 2] + [lindex $value 3]]

puts [expr [lindex $value 4] + [lindex $value 5]]

puts [expr [lindex $value 6] + [lindex $value 7]]

puts [expr [lindex $value 8] + [lindex $value 9]]

Conclusion结论

I became so attached to the stdout exit of the puts believing that only he could build the list through the list that I didn't realize I could do it any other way without these two commands.我变得如此依恋puts标准输出出口,相信只有他才能通过list构建列表,以至于我没有意识到如果没有这两个命令我可以用其他任何方式来做。

Although I tried to make use of lappend I insisted on using in conjunction with list and on top of that some erroneous attempts how to use $ followed variable, like: lapped $value $lst尽管我尝试使用lappend ,但我坚持与list结合使用,除此之外,还有一些错误尝试如何使用$后跟变量,例如: lapped $value $lst

Now I know where I was wrong, how I could have avoided so much time spent on a simple solution that it was shown in the answers.现在我知道我错在哪里了,我怎么能避免花这么多时间在一个简单的解决方案上,以至于答案中显示了它。 Anyway, the use of `lapp反正使用`lapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM