Jamie Curran
Jamie Curran

Reputation: 27

Ruby array of arrays and the map method

I started with a CSV file, which I read into a CSV::Table, 104 columns, and wanted to filter it down to three:

filtered_data = csv.map { |row| row.fields(:x,:y:,:z) }

I then want to convert x from epoch time to regular GMT. I did this using:

filtered_data.each do |thing|
  thing[0] = Time.at(thing[0]).to_datetime
end

Thus yielding:

[[converted_x, y,z],[converted_x, y, z]] 

Is there another way of doing this using the map function or is this the preferred solution?


Using Jeremy's answer I now have:

filtered_data.map { |x,y,z| [Time.at(x).to_datetime,y,z] } 

And then further filtering using reject:

filtered_data.reject { |x,y,z| [x,y, z == '\\ '] }

Upvotes: 2

Views: 2085

Answers (1)

Jeremy Roman
Jeremy Roman

Reputation: 16345

filtered_data.map do |x, y, z|
  [Time.at(x).to_datetime, y, z]
end

Upvotes: 2

Related Questions