How to print in a tabular format in tcl?

crazy_prog picture crazy_prog · Jul 13, 2011 · Viewed 20k times · Source

I am trying to print the data in a tabular format in tcl. Suppose I have three arrays:-

GOLD, TEST, DIFF and it has some values in it. I want to get in printed in the following format:-

GOLD           TEST         DIFF
-----------       --------       ---------
     1               Hello            Hi
     2               Stack          Format
     3               Guys           for
     4               TCL            print

Would you guys like to suggest something?

Answer

RHSeeger picture RHSeeger · Jul 13, 2011

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