pjw23 pjw23 - 13 days ago 7
Ruby Question

How to create and output array results

I have an array called

property_details_array
where each line of the array looks like:

["\n \n \n \n 1 W Maple Dr,\n Atlanta,\n GA\n 30315"


I'm trying to figure out the best way to clean the data for output into a table-like format, either CSV or HTML.

There are over 200 lines for each of these arrays so automating this would be extremely helpful. I started by parsing the data like:

property_details_array.each do |i|
prop_info = i.split("\n")
street = prop_info[4].strip
city = prop_info[5].strip
state = prop_info[6].strip
zip = prop_info[7].strip
end


However, I'm kind of stuck on where to go next. I've been thinking of either doing it as an array of arrays or an array of hashes, but I'm not sure if one is better than the other based on how much data I would be working with. Both methods seem to make sense, however since I'm having to clean up the data first, I'm not sure on the best way to go about it.

How do I best feed in these values for future output?

Answer

This looks like a good candidate for storage in an array of hashes as the simplest solution.

property_details_array
  .map { |row| row.split("\n") }
  .map { |prop_info| {street: prop_info[4].strip, 
                      city: prop_info[5].strip, 
                      state: prop_info[6].strip, 
                      zip: prop_info[7].strip} }