Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print in a tabular format in tcl?

Tags:

format

tcl

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?

like image 973
crazy_prog Avatar asked Jul 13 '11 09:07

crazy_prog


People also ask

How to use format command in Tcl?

The format command must be given enough args to meet the needs of all of the conversion specifiers in formatString. Each conversion specifier may contain up to six different parts: an XPG3 position specifier, a set of flags, a minimum field width, a precision, a length modifier, and a conversion character.

Which TCL command uses to print message?

The command to output a string in Tcl is the puts command. A single unit of text after the puts command will be printed to the standard output device.


1 Answers

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
like image 84
RHSeeger Avatar answered Sep 30 '22 16:09

RHSeeger