通过ruby中的2d数组循环以表格格式显示它?

时间:2011-03-06 02:05:21

标签: ruby

我如何在终端中以表格格式表示二维数组,在那里它像表一样正确排列列?

所以它看起来像这样:

         1       2       3          4          5
   1 [ Infinity | 40 | 45       | Infinity | Infinity ]
   2 [ Infinity | 20 | 50       | 14       | 20 ]
   3 [ Infinity | 30 | 40       | Infinity | 40 ]
   4 [ Infinity | 28 | Infinity | 6        | 6 ]
   5 [ Infinity | 40 | 80       | 12       | 0 ]

而不是:

[ Infinity,40,45,Infinity,Infinity ]
[ Infinity,20,50,14,20 ]
[ Infinity,30,40,Infinity,40 ]
[ Infinity,28,Infinity,6,6 ]
[ Infinity,40,80,12,0 ]

4 个答案:

答案 0 :(得分:1)

试试这个:

a = [['a', 'b', 'c'], ['d', 'e', 'f']]
puts a.map{|e| "[ %s ]" % e.join(",")}.join("\n")

修改

根据其他要求扩展答案。

a = [
  [ "Infinity",40,45,"Infinity","Infinity" ],
  [ "Infinity",20,50,14,20 ],
  [ "Infinity",30,40,"Infinity",40 ],
  [ "Infinity",28,"Infinity",6,6 ],
  [ "Infinity",40,80,12,0 ]
]

def print_2d_array(a, cs=12)
  report = []   
  report << " " * 5 + a[0].enum_for(:each_with_index).map { |e, i|
    "%#{cs}s" % [i+1, " "]}.join("   ")
  report << a.enum_for(:each_with_index).map { |ia, i|
    "%2i [ %s ]" % [i+1, ia.map{|e| "%#{cs}s" % e}.join(" | ") ] }
  puts report.join("\n")
end

<强>输出

现在调用print_2d_array(a)会产生以下结果。您可以根据需要增加列大小。

                1              2              3              4              5
 1 [     Infinity |           40 |           45 |     Infinity |     Infinity ]
 2 [     Infinity |           20 |           50 |           14 |           20 ]
 3 [     Infinity |           30 |           40 |     Infinity |           40 ]
 4 [     Infinity |           28 |     Infinity |            6 |            6 ]
 5 [     Infinity |           40 |           80 |           12 |            0 ]

答案 1 :(得分:1)

a = [[Infinity, 40, 45, Infinity, Infinity],
    [Infinity, 20, 50, 14, 20 ],
    [Infinity, 30, 40, Infinity, 40 ],
    [Infinity, 28, Infinity, 6, 6 ],
    [Infinity, 40, 80, 12, 0 ]]

分步解释

首先需要实现列宽。下面的col_width是一个数组,它给出了每列的宽度。

col_width = a.transpose.map{|col| col.map{|cell| cell.to_s.length}.max}

然后,这将为您提供表格的主要部分:

a.each{|row| puts '['+
 row.zip(col_width).map{|cell, w| cell.to_s.ljust(w)}.join(' | ')+']'}

要提供标签,请执行以下操作。

puts ' '*(a.length.to_s.length + 2)+
    (1..a.length).zip(col_width).map{|i, w| i.to_s.center(w)}.join('   ')

a.each_with_index{|row, i| puts "#{i+1} ["+
    row.zip(col_width).map{|cell, w| cell.to_s.ljust(w)}.join(' | ')+
    ']'
}

All in One 这是针对ruby1.9的。小修改应使其适用于红宝石1.8。

a
.transpose
.unshift((1..a.length).to_a)   # inserts column labels #
.map.with_index{|col, i|
    col.unshift(i.zero?? nil : i)   # inserts row labels #
    w = col.map{|cell| cell.to_s.length}.max   # w = "column width" #
    col.map.with_index{|cell, i|
         i.zero?? cell.to_s.center(w) : cell.to_s.ljust(w)}   # alligns the column #
}
.transpose
.each{|row| puts "[#{row.join(' | ')}]"}

答案 2 :(得分:0)

a = [['a', 'b', 'c'], ['d', 'e', 'f']]
a.each {|e| puts "#{e.join ", "}\n"}

可能不是最简单的方法,但有效

a, b, c
d, e, f

答案 3 :(得分:0)

好吧,如果我这样做,我会去:

require 'pp'
pp my_2d_array

但如果这是家庭作业,我认为这是行不通的。也许:

puts a.inject("") { |m, e| m << e.join(' ') << "\n" }