From: lionbarrage Date: 2009-03-05T08:48:39+09:00 Subject: Re: Help with Multi Dimensional Array On Feb 28, 12:33 am, Simon Krahnke wrote: > * lionbarrage (2009-02-27) schrieb: > > > Thanks!  I did: > > sets.keys.each{|k| > >   print k, "|" > > } > > print "\n" > > You could just do > > puts set.keys.join('|') > > To have the value output you specified in you original post, is a little > more complicated. > > > Is there a format function which will remove "" and change , to | ? > > Don't use pp for production use. Do your own formatting. Here's my > updated script: > > ,----[ sets.rb ] > | #!/usr/bin/env ruby > | > | sets = Hash.new { | h, k | h[k] = [] } # hash that contains a newarray > |                                        # for every new key > | %w(file1.txt file2).each do | filename | > |   File.open(filename) do | f | > |     names = f.gets.chop.split('|') > |     f.each do | line | > |       names.zip(line.chop.split('|')).each do | name, value | > |         sets[name] << value.to_i if value and value !~ /^\s*$/ > |       end > |     end > |   end > | end > | > | sets.values.each { | a | a.uniq! } > | > | puts sets.keys.map { | k | '%8s ' % k }.join('|') > | > | rows = sets.values.map { | a | a.size }.max > | > | (1..rows).zip(*sets.values) do | row | > |   row.shift > |   puts row.map { | v | if v then '%8s ' % v else ' '*9 end }.join('|') > | end > `---- > > ,----[ output ] > |    GAMMA |   OMEGA |   GREEK |    BETA |   ALPHA | EPSILON > |        3 |       2 |       8 |       0 |       1 |       7 > |        6 |       5 |      11 |      15 |       4 |      12 > |          |       9 |         |         |         |      10 > |          |      13 |         |         |         |         > `---- > > If you don't understand the code, please ask. > > mfg,                      simon .... l The output seems a little strange and I'm not sure why that is. Output should be ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA 1 | 2 | 3 | | | 4 | 5 | 6 | 10 | 11 | 15 | 9 | | 7 | 8 | 0 | 13 | | 12 | | instead it gets: ALPHA | OMEGA | GAMMA | EPSILON | GREEK | BETA 1 | 2 | 3 | 7 | 8 | 0 4 | 5 | 6 | 12 | 11 | 15 | 9 | | 10 | | | 13 | | | | Please advise on what's going on. Thanks