2015-10-27 95 views
0

我有两个列表我想操纵..(我是一个TCL新手..)。我想将这两个列表关联起来,并添加一些数据创建第三个列表。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

回答

0
% 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 
+0

超..谢谢! –

0

你的问题不是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是一个列表)连接成一个平面清单。

文档:concatforeachlappendlistset

+0

糟糕,没有看到你已经接受了答案。 –

+0

如果你有一些相关的和新的添加,回答一个提问者接受答案是合理的。我在这方面有很多代表,提问者接受了我认为需要不同倾向的答案。 ;-) –