Reputation: 25
I have another question, how would I combine two arrays to form two columns.
I've seen a zip command e.g.
set n $a.zip(b)
puts $output $n
However when I save the file it is all in a single line. Kindly advice.
Upvotes: 0
Views: 748
Reputation: 23140
You can emulate a zip function with lmap
:
% set a {1 2 3}
% set b {4 5 6}
% lmap x $a y $b {list $x $y}
{1 4} {2 5} {3 6}
% puts [join [lmap x $a y $b {list $x $y}] \n]
1 4
2 5
3 6
If you use a Tcl version older than 8.6, then you have to emulate lmap
itself, like for example shown here.
Upvotes: 2
Reputation: 246807
I don't think there's anything built-in, but it's simple enough to implement:
proc zip {var1 var2} {
upvar 1 $var1 A $var2 B
set zipped [list]
foreach elem1 $A elem2 $B {lappend zipped [list $elem1 $elem2]}
return $zipped
}
set a {A B C}
set b {1 2 3}
set n [zip a b] ;# ==> {A 1} {B 2} {C 3}
Upvotes: 0