0

我需要打印一个带有动态行和动态列的报告,如下所示。

            apple                           bannana                  pineapple
    Total           47              Total           376             Total 210
    a       :       3               i       :       10              m       : 45
    b       :       4               j       :       33              o       : 67
    c       :       18              k       :       245             n       : 98
    d       :       22              l       :       45
                                    x       :       43

我想取所有列表的 llength 并以列表的最高 llength 迭代 for 循环。在上面的例子中,它是 5 次。

我的疑问是,如何使用从“for 循环”中获得的数字打印列表中的特定对。

例如,在第一次迭代中,我需要选择所有水果中的第一项并打印。可能像fruit_${type}[1]。不知道我该怎么做。请指教。

    #!/bin/sh
    # \
    exec tclsh "$0" "$@"
    set fruit_apple {{a 3} {b 4} {c 18} {d 22}}
    set fruit_bannana {{i 10} {j 33} {k 245} {l 45} {x 43}}
    set fruit_pineapple {{m 45} {o 67} {n 98}}
    set fruit {apple bannana pineapple}
    puts $fruit_apple
    foreach type $fruit {
    puts $type
            foreach pair [set fruit_${type}] {
                    set key [lindex $pair 0]
                    set value [lindex $pair 1]
                    puts "$key : $value"
            }
    }
4

1 回答 1

2

您必须做很多工作才能像这样垂直格式化报告:

#! /usr/bin/env tclsh
set fruit_apple {{a 3} {b 4} {c 18} {d 22}}
set fruit_bannana {{i 10} {j 33} {k 245} {l 45} {x 43}}
set fruit_pineapple {{m 45} {o 67} {n 98}}
set fruit {apple bannana pineapple}
set maxl [expr {max([llength $fruit_apple], [llength $fruit_bannana], [llength $fruit_pineapple])}]

set fmt "%s\t%s\t%s\t"
foreach type $fruit {
    puts -nonewline [format $fmt "" $type ""]
    set total($type) 0
    foreach pair [set fruit_${type}] {
        lassign $pair key value
        incr total($type) $value
    }
}
puts ""
foreach type $fruit {
    puts -nonewline [format $fmt Total "" $total($type)]
}
puts ""
for {set i 0} {$i < $maxl} {incr i} {
    foreach type $fruit {
        set pair [lindex [set fruit_$type] $i]
        if {[llength $pair] == 2} {
            puts -nonewline [format $fmt [lindex $pair 0] : [lindex $pair 0]]
        } else {
            puts -nonewline [format $fmt "" "" ""]
        }
    }
    puts ""
}

输出:

        apple                   bannana                 pineapple               
Total           47      Total           376     Total           210     
a       :       3       i       :       10      m       :       45      
b       :       4       j       :       33      o       :       67      
c       :       18      k       :       245     n       :       98      
d       :       22      l       :       45                              
                        x       :       43                              

将数据转换为更好的数据结构稍微容易一些。在这里,使用数组:

#! /usr/bin/env tclsh
set fruit_apple {{a 3} {b 4} {c 18} {d 22}}
set fruit_bannana {{i 10} {j 33} {k 245} {l 45} {x 43}}
set fruit_pineapple {{m 45} {o 67} {n 98}}
set fruit {apple bannana pineapple}

array set keys {}
array set values {}
array set total {}
set maxl 0
foreach type $fruit {
    set l [set fruit_$type]
    if {[llength $l] > $maxl} {set maxl [llength $l]}
    set total($type) 0
    foreach pair $l {
        lassign $pair key value
        lappend keys($type) $key
        lappend values($type) $value
        incr total($type) $value
    }
}

set fmt "%s\t%s\t%s\t"
foreach type $fruit {
    puts -nonewline [format $fmt "" $type ""]
}
puts ""
foreach type $fruit {
    puts -nonewline [format $fmt Total "" $total($type)]
}
puts ""
for {set i 0} {$i < $maxl} {incr i} {
    foreach type $fruit {
        if {$i < [llength $keys($type)]} {
            puts -nonewline [format $fmt [lindex $keys($type) $i] : [lindex $values($type) $i]]
        } else {
            puts -nonewline [format $fmt "" "" ""]
        }
    }
    puts ""
}
于 2013-01-21T12:37:37.423 回答