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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With