Chris Bao
Chris Bao

Reputation: 2868

format three strings into a good pattern

In Tcl I need to output the text in the following pattern:

enter image description here

The first column is string with different length, in fact the length difference is very big, and the second and third column are two numbers.

How to use format command to control the space length between the columns, then the head of each string can stand in the same vertical line?

Upvotes: 0

Views: 512

Answers (1)

timrau
timrau

Reputation: 23058

First you need to compute expected column width. Then use format to compose the string.

Use - flag for left-justification.

For example:

puts [format "%-30s %-10s %-10s" "Column1" "Column2" "Column3"]
puts [format "%-30s %-10d %-10d" "Ben" 23 234]
puts [format "%-30s %-10d %-10d" "Afasdfasdasd" 344 324534]

can get

Column1                        Column2    Column3
Ben                            23         234
Afasdfasdasd                   344        324534

Upvotes: 2

Related Questions