2

I have an array of arrays in Ruby that i'm trying to output to a CSV file (or text). That I can then easily transfer over to another XML file for graphing.

I can't seem to get the output (in text format) like so. Instead I get one line of data which is just a large array.

0,2
0,3
0,4
0,5

I originally tried something along the lines of this

File.open('02.3.gyro_trends.text' , 'w') { |file| trend_array.each { |x,y| file.puts(x,y)}}

And it outputs

0.2
46558
0
46560
0
....etc etc.

Can anyone point me in the "write" direction for getting either:

(i) .text file that can put my data like so.

trend_array[0][0], trend_array[0][1] 
trend_array[1][0], trend_array[1][1] 
trend_array[2][0], trend_array[2][1] 
trend_array[3][0], trend_array[3][1] 

(ii) .csv file that would put this data in separate columns.
edit I recently added more than two values into my array, check out my answer combining Cameck's solution.

This is currently what I have at the moment.

trend_array=[]
j=1

# cycle through array and find change in gyro data.

while j < gyro_array.length-2

  if gyro_array[j+1][1] < 0.025 && gyro_array[j+1][1] > -0.025
    trend_array << [0, gyro_array[j][0]]
    j+=1
  elsif gyro_array[j+1][1] > -0.025  # if the next value is      increasing by x1.2 the value of the previous amount. Log it as +1
    trend_array << [0.2, gyro_array[j][0]]
    j+=1
  elsif gyro_array[j+1][1] <  0.025 # if the next value is   decreasing by x1.2 the value of the previous amount. Log it as -1
    trend_array << [-0.2, gyro_array[j][0]]
    j+=1
  end
end

#for graphing and analysis purposes (wanted to print it all as a csv  in two columns)

File.open('02.3test.gyro_trends.text' , 'w') { |file| trend_array.each { |x,y| file.puts(x,y)}}

File.open('02.3test.gyro_trends_count.text' , 'w') { |file| trend_array.each {|x,y| file.puts(y)}}

I know it's something really easy, but for some reason I'm missing it. Something with concatenation, but I found that if I try and concatenate a \\n in my last line of code, it doesn't output it to the file. It outputs it in my console the way I want it, but not when I write it to a file.

Thanks for taking the time to read this all.

3 Answers 3

3
File.open('02.3test.gyro_trends.text' , 'w') { |file| trend_array.each { |a| file.puts(a.join(","))}}
Sign up to request clarification or add additional context in comments.

3 Comments

Wicked, I knew I was missing something so simple.
@RyanAnderson as a note there is a CSV library in ruby's Standard Libraries that will facilitate this functionality for you. See CSV#add_row for more details. Writing Header will give you hints on how to implement this
Thanks engineersmnky, everyone has been really helpful. Love the stack!
1

Alternately using the CSV Class:

  def write_to_csv(row)
    if csv_exists?
      CSV.open(@csv_name, 'a+') { |csv| csv << row }
    else
      # create and add headers if doesn't exist already
      CSV.open(@csv_name, 'wb') do |csv|
        csv << CSV_HEADER
        csv << row
      end
    end
  end

  def csv_exists?
    @exists ||= File.file?(@csv_name)
  end

Call write_to_csv with an array [col_1, col_2, col_3]

2 Comments

Cameck, you're the man! Thanks a lot. Exactly what I was looking for!
Awesome! Happy to help :)
1

Thank you both @cameck & @tkupari, both answers were what I was looking for. Went with Cameck's answer in the end, because it "cut out" cutting and pasting text => xml. Here's what I did to get an array of arrays into their proper places.

require 'csv'

CSV_HEADER = [
                "Apples",
                "Oranges",
                "Pears"
                        ]

@csv_name = "Test_file.csv"

def write_to_csv(row)
  if csv_exists?
    CSV.open(@csv_name, 'a+') { |csv| csv << row }
  else
    # create and add headers if doesn't exist already
    CSV.open(@csv_name, 'wb') do |csv|
      csv << CSV_HEADER
      csv << row
    end
  end
end

def csv_exists?
  @exists ||= File.file?(@csv_name)
end

array = [ [1,2,3] , ['a','b','c'] , ['dog', 'cat' , 'poop'] ]
array.each { |row| write_to_csv(row) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.