-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix2list.rb
More file actions
40 lines (37 loc) · 1.07 KB
/
matrix2list.rb
File metadata and controls
40 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require "pp"
require "CSV"
# converts matrix data into list
def convert_data(inpath, outpath, startAt)
CSV.open(outpath, "wb", :headers => true, :col_sep => ',') do |out|
matrix= CSV.read(inpath, :headers => true, :col_sep => ',')
# create list headers from matrix headers
headerrow= Array.new
for idx in 0..startAt-1
headerrow << matrix.headers()[idx]
end
headerrow << "column"
headerrow << "value"
out << headerrow
# convert each matrix row into list rows
matrix.each do |row|
baserow= Array.new
for idx in 0..startAt-1
baserow << row.field(idx)
end
for idx in startAt..row.length()-1
# do not generate list row when matrix field is empty
if row.field(idx)!=nil
outrow = Array.new(baserow)
outrow << row.headers[idx]
outrow << row.field(idx)
out << outrow
end
end
end
end
end
unless ARGV.length() == 3
pp "usage: matrix2list.rb inpath outpath startAt"
else
convert_data(ARGV[0],ARGV[1],ARGV[2].to_i)
end