2015-06-15 115 views
0

有一個TCL列表看起來像以下(格式化爲更好的可視性):漂亮的打印一個TCL列表

set mylist [list \ 
    "title_1     title_2  title_3 " \ 
    "row1col1_bla   row1col2  row1col3 " \ 
    "r2c1     r2c2_blablabla r2c3" \ 
    "r3c1_really_long_string r3c2   r3c3" \ 
] 

我需要打印$mylist像一個過程:

title_1  title_2 title_3 
1) row1col1_bla row1col2 row1col3 
2) r2c1   &FN_1 r2c3 
3) &FN_2  r3c2  r3c3 

    Footnotes: 
    FN_1: r2c2_blablabla 
    FN_2: r3c1_really_long_string 

程序應該作爲輸入:

  • COLMAXLEN:格言嗯從$mylist任何單個串,其中beyound該字符串將去Footnotes部的長度(由$SHORTCUT_$index值替換)
  • SHORTCUT:字符串替換的$mylist任何單個成員,如果其長度大於$COLMAXLEN更大。

下面的函數就是這樣做的。有沒有可能簡化或改進它的建議?

#!/usr/bin/tclsh 

set COLMAXLEN 12 
set SHORTCUT "FN" 

proc puts_list {mylist} { 
    global COLMAXLEN SHORTCUT 
    set num_row [llength $mylist] 
    set num_col [llength [lindex $mylist 0]] 
    set ref_list {} 

    # Define/init col_width (a list having $num_col elements) 
    set col_width {} 
    for {set col 0} {$col < $num_col} {incr col} { 
     lappend col_width 0 
    } 

    # Get the max width of each column AND 
    # replace the elements > $COLMAXLEN with footnote shortcuts! 
    for {set row 0} {$row < $num_row} {incr row} { 
     set new_row {} 
     for {set col 0} {$col < $num_col} {incr col} { 
      set myrow [lindex $mylist $row] 
      set myitem [lindex $myrow $col] 
      set mysize [string length $myitem] 
      if { $mysize > $COLMAXLEN } { 
       lappend ref_list $myitem 
       set myitem "&[subst $SHORTCUT]_[llength $ref_list]" 
       set mysize [string length $myitem] 
      } 
      if { $mysize > [lindex $col_width $col] } { 
       lset col_width $col $mysize 
      } 
      lappend new_row $myitem 
     } 
     lset mylist $row $new_row 
    } 

    # Start printing 
    set num_col_width [expr [string length $num_row] +1] 
    puts "" 
    for {set row 0} {$row < $num_row} {incr row} { 
     if { $row == 0 } { 
      puts -nonewline [format "%[subst $num_col_width]s" { }] 
     } else { 
      puts -nonewline [format "%[subst $num_col_width]s" "$row)"] 
     } 
     puts -nonewline " " 
     for {set col 0} {$col < $num_col} {incr col} { 
      set myrow [lindex $mylist $row] 
      set myitem [lindex $myrow $col] 
      set mysize [expr [lindex $col_width $col] +1] 
      puts -nonewline [format "%-[subst $mysize]s" $myitem] 
     } 
     puts "" 
    } 
    puts "" 
    puts " Footnotes:" 
    set ref_num [llength $ref_list] 
    for {set i 0} {$i < $ref_num} {incr i} { 
     puts " [subst $SHORTCUT]_[format %-[subst [string length $ref_num]]s [expr $i + 1]]: [lindex $ref_list $i]" 
    } 
    puts "" 
} 

的假設:TCL的版本是8.4

+2

這類問題(已工作,尋找改進)應該被移動到[代碼審查(http://codereview.stackexchange.com /)。這就是說,我正在閱讀... –

+0

@HaiVu你是對的!其實,我可以理解這樣的請求/問題的低利益!然而,我最初的意圖是分配一個賞金(我的第一個)。然後,我看到我必須等待2天才提供賞金!而且,坦白地說,我沒有明確的理由就陷入低估。非常感謝你的回覆! –

回答

1
# Print the row number 
proc put_row_number {rowNumber} { 
    if {$rowNumber == 0} { 
     puts -nonewline " " 
    } else { 
     puts -nonewline "$rowNumber) " 
    } 
} 

proc put_table {table {colmaxlen 12} {shortcut FN}} { 
    set ref {} 
    set refCount 0 
    set rowCount 0 

    foreach row $table { 
     put_row_number $rowCount 
     incr rowCount 
     foreach cell $row { 
      if {[string length $cell] > $colmaxlen} { 
       incr refCount 
       set key "${shortcut}_${refCount}" 
       lappend ref $key $cell 
       set cell "&$key" 
      } 
      puts -nonewline [format "%-*s " $colmaxlen $cell] 
     } 
     puts "" 
    } 

    puts "\n Footnotes:" 
    foreach {key value} $ref { 
     puts " $key: $value" 
    } 
} 

set mylist { 
    "title_1     title_2  title_3 " 
    "row1col1_bla   row1col2  row1col3 " 
    "r2c1     r2c2_blablabla r2c3" 
    "r3c1_really_long_string r3c2   r3c3" 
} 
put_table $mylist 
+0

太棒了!看起來很簡單!當我到達臺式電腦(從手機發布)時,我會試一試。感謝路過! –