Moving through a 2d array in ruby ​​to display it in table format?

How can I represent a 2d array in table format in a terminal, where it aligns columns correctly just like a table?

so that it looks like this:

         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 ]

instead:

[ 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 ]
+3
source share
4 answers
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 ]]

Step by Step Explanation

First you need to get the column width. col_widthbelow is an array that gives the width for each column.

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

This will then give you the bulk of the table:

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

To label, do the following.

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. This is for ruby1.9. A small modification should make it work with Ruby 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(' | ')}]"}
+1
source

Try the following:

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

Edit:

The extended answer is based on an additional request.

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

Output

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
a = [['a', 'b', 'c'], ['d', 'e', 'f']]
a.each {|e| puts "#{e.join ", "}\n"}

,

a, b, c
d, e, f
0

, , :

require 'pp'
pp my_2d_array

But if this is homework, I believe that it will not work. Maybe:

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

Source: https://habr.com/ru/post/1796575/


All Articles