2

I'm trying to parse a csv file with 16 columns to 16 separate arrays. I need each cell to be another object in the array. So the values in column 1 becomes arr1, column 2 becomes arr2, etc. This is the code I have so far:

file = "FS_Email_Test.csv"

arr1 = []
arr2 = []
arr3 = []
list =CSV.foreach(file, :col_sep => ";", :return_headers => false) do |row|
        arr1 << row[0].to_i
        arr2 << row[1].to_i
        arr3 << row[2].to_s
      end
puts arr1

This code correctly parses column1 into arr1, but it returns 0 values for arr2 and arr3. I need it to work for each column. Ideas/thoughts? Thanks for the help.

4
  • can you please give us some example content of the CSV file? Commented Jul 25, 2013 at 21:39
  • Going to need to see some example input to get an idea of what's going on here, preferably actual input. Commented Jul 25, 2013 at 22:12
  • Sure. Email, ID Number, [email protected], 2, [email protected], 1. The content is pretty irrelevant as it will change from time to time. I just need everything that is in the first column, Column A to become one big array. Then I need everything in the second column, Column B to become a separate array. So on and so forth for 16 columns. Commented Jul 26, 2013 at 2:49
  • Problem solved- I just needed to remove the .to_i and .to_s. I will answer the question when time permits. Commented Jul 26, 2013 at 3:06

1 Answer 1

2

Problem solved. There was an issue with the .to_i and .to_s on the end of the arrays. I took that piece off and the code works just fine. Thanks for the help.

Code:

file = "FS_Email_Test.csv"

arr1 = []
arr2 = []
arr3 = []
list =CSV.foreach(file, :col_sep => ";", :return_headers => false) do |row|
        arr1 << row[0]
        arr2 << row[1]
        arr3 << row[2]
      end
puts arr1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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