Reputation: 87
I can't figure out how to write an array vertically down a column to my output CSV file. Writing an array horizontally is simple enough and can be done in one line:
CSV.open("log.csv", "wb") do |csv|
csv << ["Computers",]
csv << myarray
end
Is there any way to get myarray
written down a column in one line, or is it more complicated than that?
Array format:
["pc_0", "pc_1","pc_2"]
Upvotes: 1
Views: 775
Reputation: 106027
Loop over your array and add each item to the CSV as an array:
CSV.open("log.csv", "wb") do |csv|
csv << [ "Computers" ]
MyArray.each do |item|
csv << [ item ]
end
end
Upvotes: 1
Reputation: 7685
You could iterate through the array with each
.
CSV.open("log.csv", "wb") do |csv|
csv << ["Computers"]
myArray.each { |e| csv << e }
end
If you have several arrays to put into the CSV file, you could use zip
to put all together in on array.
CSV.open("log.csv", "wb") do |csv|
csv << ["Computers","OS","RAM"]
arr = myComputers.zip(myOperatingSystems, myRams)
arr.each { |row| csv << row }
end
Upvotes: 0
Reputation: 74
Maybe you can use Marshal dump to serialize array then convert string to insert csv.
Upvotes: 0