Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

join values in two lists

Status
Not open for further replies.

georginaR

Technical User
Apr 11, 2012
2
AU
Hi all,

I have two lists of variables that I need to combine for use in a foreach command in the following way, but can not come up with the right coding/function.

list 1 {a b c d e}
list 2 {x y z}
combine into new list
list 12 {ax bx cx dx ex ay by cy dy ey az bz cz dz ez}

Any help would be greatly appreciated
 
Code:
[COLOR=#804040][b]set[/b][/color] list1 {a b c d e}
[COLOR=#804040][b]set[/b][/color] list2 {x y z}

[COLOR=#0000ff]#list12 {ax bx cx dx ex ay by cy dy ey az bz cz dz ez}[/color]
[COLOR=#804040][b]set[/b][/color] list12 {}
[COLOR=#804040][b]foreach[/b][/color] el2 [COLOR=#008080]$list2[/color] {
  [COLOR=#804040][b]foreach[/b][/color] el1 [COLOR=#008080]$list1[/color] {
    [COLOR=#804040][b]lappend[/b][/color] list12 [COLOR=#ff00ff]"[/color][COLOR=#008080]$el1$el2[/color][COLOR=#ff00ff]"[/color]
  }
}

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]list1 = [/color][COLOR=#6a5acd]\[[/color][COLOR=#008080]$list1[/color][COLOR=#6a5acd]\][/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]list2 = [/color][COLOR=#6a5acd]\[[/color][COLOR=#008080]$list2[/color][COLOR=#6a5acd]\][/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]list12= [/color][COLOR=#6a5acd]\[[/color][COLOR=#008080]$list12[/color][COLOR=#6a5acd]\][/color][COLOR=#ff00ff]"[/color]
Output:
Code:
C:\_mikrom\Work>tclsh georginaR.tcl
$list1 = [a b c d e]
$list2 = [x y z]
$list12= [ax bx cx dx ex ay by cy dy ey az bz cz dz ez]
 
Thanks Heaps! Thats exactly what I wanted.
I was missing the second foreach.... Oops.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top