输出列表项目和LS一样

发布于 2025-02-12 00:02:32 字数 725 浏览 1 评论 0原文

我正在尝试输出类似于LS输出的东西。这样的LS命令输出:

file1.txt   file3.txt    file5.txt
file2.txt   file4.txt

但是我此示例列表:

a b c d e f g h i j k l m n o p q r s t u v w x y z

显示为:

a e i m q u y
b f j n r v z
c g k o s w 
d h l p t x

在这种情况下,它提供了7列,但我最多想多达8列最大。下一个列表:

a b c d e f g h i j k l m n o p q r s t u v w

必须显示为:

a d g j m p s v
b e h k n q t w
c f i l o r u

和“ ABCDEFG H”,必须按以下显示,因为它已经是1行中的8列,但是:

a b c d e f g h i

将显示为:

a c e g i 
b d f h

和::::::

a b c d e f g h i j

a c e g i 
b d f h j

I'm trying to output something that resembles as ls output. The ls command outputs like this:

file1.txt   file3.txt    file5.txt
file2.txt   file4.txt

But I this sample list:

a b c d e f g h i j k l m n o p q r s t u v w x y z

to appear as:

a e i m q u y
b f j n r v z
c g k o s w 
d h l p t x

In that case, it gave 7 columns which is fine, but I wanted up to 8 columns max. Next the following list:

a b c d e f g h i j k l m n o p q r s t u v w

will have to show as:

a d g j m p s v
b e h k n q t w
c f i l o r u

And "a b c d e f g h" will have to show as is because it is already 8 columns in 1 line, but:

a b c d e f g h i

will show as:

a c e g i 
b d f h

And:

a b c d e f g h i j

a c e g i 
b d f h j

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2025-02-19 00:02:33

一种方法:

#!/usr/bin/env tclsh                                                                                                                                                                                                                             

proc columnize {lst {columns 8}} {
    set len [llength $lst]
    set nrows [expr {int(ceil($len / (0.0 + $columns)))}]
    set cols [list]
    for {set n 0} {$n < $len} {incr n $nrows} {
        lappend cols [lrange $lst $n [expr {$n + $nrows - 1}]]
    }
    for {set n 0} {$n < $nrows} {incr n} {
        set row [list]
        foreach col $cols {
            lappend row [lindex $col $n]
        }
        puts [join $row " "]
    }
}


columnize {a b c d e f g h i j k l m n o p q r s t u v w x y z}
puts ----
columnize {a b c d e f g h i j k l m n o p q r s t u v w}
puts ----
columnize {a b c d e f g h}
puts ----
columnize {a b c d e f g h i}
puts ----
columnize {a b c d e f g h i j}

columnize函数首先要通过请求的列数的简单划分列表的简单划分,然后将列表划分为该长度的块,一个一个,一个每个列,最后通过那些为每个列提取当前行元素的订阅者迭代,并将行以空格分隔列表打印出来。

One way:

#!/usr/bin/env tclsh                                                                                                                                                                                                                             

proc columnize {lst {columns 8}} {
    set len [llength $lst]
    set nrows [expr {int(ceil($len / (0.0 + $columns)))}]
    set cols [list]
    for {set n 0} {$n < $len} {incr n $nrows} {
        lappend cols [lrange $lst $n [expr {$n + $nrows - 1}]]
    }
    for {set n 0} {$n < $nrows} {incr n} {
        set row [list]
        foreach col $cols {
            lappend row [lindex $col $n]
        }
        puts [join $row " "]
    }
}


columnize {a b c d e f g h i j k l m n o p q r s t u v w x y z}
puts ----
columnize {a b c d e f g h i j k l m n o p q r s t u v w}
puts ----
columnize {a b c d e f g h}
puts ----
columnize {a b c d e f g h i}
puts ----
columnize {a b c d e f g h i j}

The columnize function first figures out how many rows are needed with a simple division of the length of the list by the number of columns requested, then splits the list up into chunks of that length, one per column, and finally iterates through those sublists extracting the current row's element for each column, and prints the row out as a space-separated list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文