TCL数组或列表对变量的操作
以下TCL函数被调用n次。每次将numid和type传递给这个函数时,我都会尝试为每个类型lappend numid 比如说 如果传递的值如下所示TCL数组或列表对变量的操作,tcl,Tcl,以下TCL函数被调用n次。每次将numid和type传递给这个函数时,我都会尝试为每个类型lappend numid 比如说 如果传递的值如下所示 2 BLACK 1 RED 1 BLACK 3 BLUE 1 BLUE 2 BLUE 2 RED 我得到的输出是使用set_numid_type函数 black_color_str 2 1 red_color_str 1 2 blue_color_str 3 1 2
2 BLACK
1 RED
1 BLACK
3 BLUE
1 BLUE
2 BLUE
2 RED
我得到的输出是使用set_numid_type函数
black_color_str 2 1
red_color_str 1 2
blue_color_str 3 1 2
但我需要的输出如下。当类型不按顺序排列时,应将其追加到不同的变量类型
black_color_str 2
red_color_str 1
black_color_str 1
blue_color_str 3 1 2 (since BLUE color is called in sequence)
red_color_str 2
proc set_numid_type {numid type} {
variable black_color_str
variable red_color_str
variable blue_color_str
if {$type == "BLACK"} {
if {![info exists black_color_str] || ![llength $ black_color_str]} {
set black_color_str ""
}
lappend black_color_str $numid
}
if {$type == "RED"} {
if {![info exists red_color_str] || ![llength $ red_color_str]} {
set red_color_str ""
}
lappend red_color_str $numid
}
if {$type == "BLUE"} {
if {![info exists blue_color_str] || ![llength $ blue_color_str]} {
set blue_color_str ""
}
lappend blue_color_str $numid
}
}
这是一个对输入列表进行操作并返回输出列表的版本。 如果确实需要,可以修改它以处理全局变量
proc GroupSequences { inputPairs } {
if {[llength $inputPairs] == 0} return {}
set sequences {}
set lastColour ""
set latestSequence {}
foreach pair $inputPairs {
puts 1
if {"[lindex $pair 1]" != $lastColour} {
puts 2
if {[llength $latestSequence] > 0} {
puts 3
lappend sequences $latestSequence
}
puts 4
set latestSequence [lreverse $pair]
set lastColour [lindex $pair 1]
} else {
puts 5
lappend latestSequence [lindex $pair 0]
}
}
puts 6
lappend sequences $latestSequence
return $sequences
}
set input [list { 2 "black" } { 1 "red" } { 1 "black" } {3 "blue" } { 1 "blue" } { 2 "blue" } { 2 "red" } ]
set seq [GroupSequences $input]
建议张贴呼叫代码以及。这只是一个管理最后一种颜色的状态的问题,以确定是否应将当前颜色添加到序列或创建新行。调用代码来自xslt端,如下所示:@tcluser请解释如何使用数据。您是否计划在调用过程中使用变量?您需要打印输出吗?Hi Nir,我希望实现以下输出,我将传递到下一个函数black\u color\u str 2 red\u color\u str 1 black\u color\u str 1 blue\u color\u str 3 1 2(因为蓝色是按顺序调用的)red_color_str 2如果类型未按顺序传递,则应将其附加到不同的变量。我不想打印输出。您好,Ergwun,感谢您抽出时间回答我的问题,但我特别希望以我在所需输出中描述的方式附加变量。我只希望每个变量只附加在numid上。我正在尝试在编辑器中修复您的代码,但如果您可以根据我正在寻找的需求更新您的代码,这将是非常棒的。再次感谢。抱歉,没有。您已经发布的代码显示了如何附加到过程外部的变量。如果有一个特定的部分你不明白,那么再问另一个问题。