Sideways Cat
11/Mar 2015
Data manipulation in Ruby
A friend asked me to wrote some code that would transform a collection of files like this:
File 1
Key | Value1 |
---|---|
A | 1.0 |
B | 2.0 |
C | 3.0 |
File 2
Key | Value2 |
---|---|
A | 3.0 |
B | 2.0 |
C | 1.0 |
Into a format like this:
Key | Value1 | Value2 |
---|---|---|
A | 1.0 | 3.0 |
B | 2.0 | 2.0 |
C | 3.0 | 1.0 |
Except, there were many files and many rows so I wrote a ruby script.
#!/bin/ruby
files = Dir.glob('Level_4/T*')
data = Array.new
files.each_with_index do |f, fi| # file index
File.open(f, 'r') do |file|
file.each_with_index do |l, i| # row index
rn, rv = l.chomp.split(/\t/)
#puts "rn: #{rn} rv: #{rv}"
data[0] = Array.new if data[0].nil?
data[fi+1] = Array.new if data[fi+1].nil?
data[0][i] = rn # repeats
data[fi+1][i] = rv
end
end
end
xdata = Array.new
data.each_with_index do |row, ri|
row.each_with_index do |e, i|
if xdata[i].nil?
xdata[i] = Array.new
end
xdata[i][ri] = e
end
end
xdata.each do |r|
puts r.join("\t")
end
More Reading