在tcl中,这种代码的写法比较有参考价值
proc fmtable table {
set maxs {}
foreach item [lindex $table 0] {
lappend maxs [string length $item]
}
foreach row [lrange $table 1 end] {
set i 0
foreach item $row max $maxs {
if {[string length item]>item]>item]>max} {
lset maxs $i [string length $item]
}
incr i
}
}
set head +
foreach max $maxs {append head -[string repeat - $max]-+}
set res $head\n
foreach row $table {
append res |
foreach item $row max KaTeX parse error: Expected '}', got 'EOF' at end of input: …es [format " %-{max}s |" $item]}
append res \n
}
append res $head
}
下面是测试代码
fmtable { {1 short “long field content”} {2 “another long one” short} {3 “” hello} }
结果
±–±-----------------±-------------------+
| 1 | short | long field content |
| 2 | another long one | short |
| 3 | | hello |
±–±-----------------±-------------------+
proc transpose matrix {
foreach row $matrix {
set i 0
foreach el $row {
lappend [incr i] $el
}
}
set res {}
set i 0
foreach e [lindex $matrix 0] {
lappend res [set [incr i]]
}
set res
}