Hi all,
I'm having trouble with a hash output.
The original files are:
File 1
ALPHA>OMEGA>GAMMA
1|2|3
4|5|6
File 2
EPSILON>GREEK>OMEGA>BETA
7|8|9|0
12||13|
10|11|5|15
and I'm using the following code:
sets = Hash.new { | h, k | h[k] = [] } # hash that contains a new
array
# 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
but 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