0

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?


New Info

@tadman - Sorry for the lack of explanation and thank you very much for your help. What I meant to say was, After looking at the data closer, I realized the results are not in a predictable order. Sometimes price will be at [16], other times it's not. I'm back to the drawing board on trying to figure out how to get results into an array of hashes.

The raw data I'm working with looks like:

["\n      \n        \n          \n                2265 Tanglewood Cir NE,\n            Atlanta,\n            GA\n            30345\n \n        \n\n        \n          Dresden East\n        \n        \n\n            $289,900\n          \n          \n            \n        3 bd\n                2 ba\n                1,566 sq ft\n             
0.3 acres lot\n            \n          \n        \n          \n            Single Family Home\n          \n        \n          \n            \n  
Brokered by Re/Max Town And Country\n            \n          \n       
\n        \n          \n            Brokered by \n            Re/Max
Town And Country\n          \n        \n      \n    ",  "\n      \n   
\n          \n                2141 Dunwoody Gln,\n           
Atlanta,\n            GA\n            30338\n          \n        \n\n 
\n          \n            $469,900\n          \n          \n          
\n                4 bd\n                3 ba\n                2,850 sq
ft\n                0.3 acres lot\n                2 car\n           
\n          \n        \n          \n            Single Family Home\n  
\n        \n          \n            \n              Brokered by
Buckhead Home Realty Llc\n            \n          \n        \n       
\n          \n            Brokered by \n            Buckhead Home
Realty Llc\n          \n        \n      \n    ",  "\n      \n       
\n          \n                1048 Martin St SE,\n           
Atlanta,\n            GA\n            30315\n          \n        \n\n 
\n          Intown South\n          Peoplestown\n        \n        \n 
\n            $164,900\n          \n          \n            \n        
5 bd\n                3 ba\n                2,376 sq ft\n             
7,405 sq ft lot\n            \n          \n        \n          \n     
Single Family Home\n          \n        \n          \n            \n  
Brokered by Greenlet Llc\n            \n          \n        \n       
\n          \n            Brokered by \n            Greenlet Llc\n    
\n        \n      \n    ",  "\n      \n        \n          \n         
1048 Martin St SE,\n            Atlanta,\n            GA\n           
30315\n          \n        \n\n        \n          Intown South\n     
Peoplestown\n        \n        \n          \n            $164,900\n   
\n          \n            \n                5 bd\n                3
ba\n                2,055 sq ft\n                7,584 sq ft lot\n    
\n          \n        \n          \n            Single Family Home\n  
\n        \n          \n            \n              Brokered by
Greenlet, Llc\n            \n          \n        \n        \n         
\n            Brokered by \n            Greenlet, Llc\n          \n   
\n      \n    "]

Ideally, I'm trying to parse out the following: (Street, City, State, Zip, Price, BD, BA, Sq.FT) Any ideas on the best way to do this.

2 Answers 2

2

Why not split out fields into a Hash if they're in a predictable order?

You can do this:

FIELDS = [ nil, nil, nil, nil, :street, :city, :state, :zip ]

details.map do |d|
  Hash[
    FIELDS.zip(d.split("\n").map(&:strip)).select do |key, value|
      key
    end
  ]
end
# => [{:street=>"1 W Maple Dr,", :city=>"Atlanta,", :state=>"GA", :zip=>"30315"}]

That constructs a an array of hashes each containing any fields that could be mapped. The advantage here is if your input format changes, if the fields get rearranged, your output can be consistent.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply, the problem using this approach is that the list is not properly comma separated. I don't have enough ruby knowledge to make it to work properly...
What do you mean "not properly"? I'm not sure what to make of that remark. This processes the sample data that was given.
Sorry for not explaining better, I'm still getting a hold of Ruby and how to work with it. I've added additional information to the original post...If you can provide any help on this I would be extremely grateful. Thanks!
0

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} }

2 Comments

Thank you so much for your reply. I think this is what I'm looking for.
Sure thing. For the record, the answer by @tadman will produce the same result. The difference is that instead of hardcoding the field order, as my answer does, he puts the column names in a constant that would allow you to add, reorder, or remove fields a bit more easily.

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.