A cheap hacking method is to use tabs as delimiters (" \t " instead of " ") in your output line. This will work with small changes, but will not handle wide variations (or small changes around the current width of the terminal / editor tab).
To do the job properly, you first need to get a list of all the conceptual lines that you want to print (i.e. data, but not formatted yet). Then you look at each row and set the width required for each field, taking the maximum across the entire data set. In doing so, you can configure the format string for format . Here is an example (for Tcl 8.5) where everything is just formatted as strings:
proc printColumnarLines {lines} { foreach fields $lines { set column 0 foreach field $fields { set w [string length $field] if {![info exist width($column)] || $width($column) < $w} { set width($column) $w } incr column } } foreach fields $lines { set column 0 foreach field $fields { puts -nonewline [format "%-*s " $width($column) $field] incr column } puts "";
* in the format string at this position means accepting another argument that determines the width of this field. I am not surprised that you missed it; formatted strings are actually a very dense micro language and itβs easy to skip an important bit. (This is true for all other languages ββthat use them; very few people know everything you can do with them.)
You can do much more intelligent things with fixed sets of fields, and other % sequences support * too. Keep in mind, I usually have to experiment to get exactly what I want (especially with floating point numbers ...)
source share