How to display output in form in tcl

I have doubts about the format command. my output is like a cluster and uneven as below

  24-04-2011 16:07 <DIR> Administrator 15-05-2011 16:05 <DIR> confuser 01-02-2011 20:57 <DIR> Public 

What should I do to display the output correctly and uniformly. all at the beginning in the same column. eg:

Yes, I used this command puts [format {%-s %-4s %-8s} $user\t $date\t $time] , giving me the output, for example:

 Administrator 15-05-2011 16:05 confuser 01-02-2011 20:57 Public 29-01-2011 19:28 TechM 30-04-2011 09:47 

The resulting output is taken in accordance with the number of letters present on the first line, for example, by the administrator of confuser public techm. so I need to know how to get a result that does not take into account the length of the first row and gives the correct uniform output of the columns.

+4
source share
3 answers

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 ""; # Just the newline please } } 

* 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 ...)

+7
source

The easiest way to solve this problem is to use the struct :: matrix package, part of tcllib:

 package require struct::matrix set lines { {Administrator 15-05-2011 16:05} {confuser 01-02-2011 20:57} {Public 29-01-2011 19:28} {TechM 30-04-2011 09:47} {"Name with space" 29-04-2011 11:05} } struct::matrix m; # Create a new matrix m add columns 3; # The matrix has 3 columns: file name, date, and time foreach line $lines { m add row $line; # Add a line to the matrix } m format 2chan; # Prints it out 

Output:

 Administrator 15-05-2011 16:05 confuser 01-02-2011 20:57 Public 29-01-2011 19:28 TechM 30-04-2011 09:47 Name with space 29-04-2011 11:05 

Notes:

  • If the field contains a space, this field must be enclosed in double quotation marks (see "Name with a space")
  • format 2chan format only for format fields left justified. If you are looking at center or right alignment, find the report package.
+4
source

First you must collect all the records displayed, then find the size of the longest row to be placed in the first output column; call him max_len . Then, knowing this size, you insert each row that will be displayed in the first column, with so many spaces on the right side (at the end of the line, that is), so that the total total length is exactly max_len (hint: use string repeat " " [expr {$max_len - [string length $s]}] to create a fill block), then use this add-on to output the string.

By the way, you can see how the standard parray command is parray , as it does exactly what you want. To see its implementation, do

 set x {Administrator foo Molly bar Dolly blorb} parray x info body parray 

at the Tcl shell interactive prompt ( tkcon is recommended for this).

+1
source

All Articles