tcl 遍历多个列表
tcl loop through multiple lists
我有两个要操作的列表..(我是 tcl 新手..)。我想关联这两个列表并创建第三个列表并添加一些数据。
我拥有的数据:
set aes {ae0 ae3 ae6 ae1v1 ae1v8}
set c {c1 c2 c3 k1 k2}
foreach A $aes {
foreach C $c {
puts ${A}_$C
}
}
如您所料,我得到的数据是:
ae0_c1
ae0_c2
ae0_c3
ae0_k1
ae0_k2
..
..
我想做的是
在此之前附加一些数据。
AE-To-c = All ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 .. End
.
% set aes {ae0 ae3 ae6 ae1v1 ae1v8}
ae0 ae3 ae6 ae1v1 ae1v8
% set c {c1 c2 c3 k1 k2}
c1 c2 c3 k1 k2
% foreach A $aes {
foreach C $c {
# saving into 'result' variable
lappend result ${A}_${C}
}
}
% set data "some more here"
some more here
% set result
ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2
% set result [linsert $result 0 $data]
some more here ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2
您的问题并非 100% 清楚。你想要这样的东西吗?
set res [list AE-To-c = All]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
lappend res End
如果你想做我认为你想做的事情,你需要在一个列表中捕获两个列表的排列而不是打印出来,然后用前缀和后缀包裹那个列表。
上述方法使用 AE-To-c = All
前缀预加载结果列表,然后使用 lappend
获取排列,最后添加 End
后缀作为最后一个元素列表。
另一种方式:
set res [list]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
concat [list AE-To-c = All] $res End
在此变体中,首先创建排列列表,然后将前缀列表、排列列表和后缀列表(是的,End
是一个列表)连接成一个平面列表。
我有两个要操作的列表..(我是 tcl 新手..)。我想关联这两个列表并创建第三个列表并添加一些数据。
我拥有的数据:
set aes {ae0 ae3 ae6 ae1v1 ae1v8}
set c {c1 c2 c3 k1 k2}
foreach A $aes {
foreach C $c {
puts ${A}_$C
}
}
如您所料,我得到的数据是: ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 .. ..
我想做的是
在此之前附加一些数据。
AE-To-c = All ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 .. End
.
% set aes {ae0 ae3 ae6 ae1v1 ae1v8}
ae0 ae3 ae6 ae1v1 ae1v8
% set c {c1 c2 c3 k1 k2}
c1 c2 c3 k1 k2
% foreach A $aes {
foreach C $c {
# saving into 'result' variable
lappend result ${A}_${C}
}
}
% set data "some more here"
some more here
% set result
ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2
% set result [linsert $result 0 $data]
some more here ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2
您的问题并非 100% 清楚。你想要这样的东西吗?
set res [list AE-To-c = All]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
lappend res End
如果你想做我认为你想做的事情,你需要在一个列表中捕获两个列表的排列而不是打印出来,然后用前缀和后缀包裹那个列表。
上述方法使用 AE-To-c = All
前缀预加载结果列表,然后使用 lappend
获取排列,最后添加 End
后缀作为最后一个元素列表。
另一种方式:
set res [list]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
concat [list AE-To-c = All] $res End
在此变体中,首先创建排列列表,然后将前缀列表、排列列表和后缀列表(是的,End
是一个列表)连接成一个平面列表。