How to print in a tabular format in tcl?
First convert the arrays into lists:
set GOLDList ""
set keyList [array names GOLD]
foreach key $keyList {
lappend GOLDList $GOLD($key)
}
Then you can use the foreach snippet:
set GOLDList "1 2 3 4"; #1st list
set TESTList "Hello Stack Guys TCL"; #2nd list
set DIFFList "Hi Format for print"; #3rd list
foreach c1 $GOLDList c2 $TESTList c3 $DIFFList {
puts $c1\t$c2\t$c3
}
That is the output (you need to print the header extra)
1 Hello Hi
2 Stack Format
3 Guys for
4 TCL print
I would use the format command combined with foreach to accomplish what you're asking for. I'm assuming you actually have 3 lists, not 3 arrays, since it would appear the values of gold, test, diff are related to each other in some way.
set goldList {1 2 3 4}
set testList {Hello Stack Guys TCL}
set diffList {Hi Format for print}
set formatStr {%15s%15s%15s}
puts [format $formatStr "GOLD" "TEST" "DIFF"]
puts [format $formatStr "----" "----" "----"]
foreach goldValue $goldList testValue $testList diffValue $diffList {
puts [format $formatStr $goldValue $testValue $diffValue]
}
# output
GOLD TEST DIFF
---- ---- ----
1 Hello Hi
2 Stack Format
3 Guys for
4 TCL print
Here's code that does what you want with a single foreach loop. There's no need to create temporary lists - assuming you have common indexes for the arrays (you didn't specify).
array set GOLD {a 1 b 2 c 3 d 4}
array set TEST {d TCL c Guys b Stack a Hello}
array set DIFF {a Hi c for b Format d print}
foreach idx [lsort [array names GOLD]] {
puts "$GOLD($idx)\t$TEST($idx)\t$DIFF($idx)"
}
If you don't have common indexes for the arrays (then I wonder question the utility of the printed table), you can do this (though the relative ordering is undefined):
foreach {gidx gval} [array get GOLD] {tidx tval} [array get TEST] {didx dval} [array get DIFF] {
puts "$gval\t$tval\t$dval"
}