Reputation: 190
I have an array as shown below:
[
[
"[\"\", \"Mrs. Brain Bauch\", \"Vernice Ledner\"]",
"[\"\", \"Robb Ratke\", \"Amaya Jakubowski\"]",
"[\"\", \"Lindsey Cremin III\", \"Harvey Fisher\"]",
"[\"\", \"Daniela Schneider\", \"Benny Schumm\"]"
]
]
How can I convert this into the array structure shown below:
[
[
["Mrs. Brain Bauch", "Vernice Ledner"],
["Robb Ratke", "Amaya Jakubowski"],
["Lindsey Cremin III", "Harvey Fisher"],
["Daniela Schneider", "Benny Schumm"]
]
]
Upvotes: 1
Views: 1239
Reputation: 121000
require 'json'
input = [[
"[\"\", \"Mrs. Brain Bauch\", \"Vernice Ledner\"]",
"[\"\", \"Robb Ratke\", \"Amaya Jakubowski\"]",
"[\"\", \"Lindsey Cremin III\", \"Harvey Fisher\"]",
"[\"\", \"Daniela Schneider\", \"Benny Schumm\"]"
]]
[input.first.map { |l| JSON.parse l }.map { |a| a.reject &:empty? }]
#⇒ [[
# ["Mrs. Brain Bauch", "Vernice Ledner"],
# ["Robb Ratke", "Amaya Jakubowski"],
# ["Lindsey Cremin III", "Harvey Fisher"],
# ["Daniela Schneider", "Benny Schumm"]
# ]]
Upvotes: 5
Reputation: 110675
If arr
is your array:
r = /
(?<=\") # match `\"` in a positive lookbehind
[A-Z] # match a capital letter
[a-z\.\s]+ # match a letter, period or space one or more times
/ix # case-insenitive (i) and free-spacing (x) regex definition modes
[arr.first.map { |s| s.scan r }]
#=> [[["Mrs. Brain Bauch", "Vernice Ledner"],
# ["Robb Ratke", "Amaya Jakubowski"],
# ["Lindsey Cremin III", "Harvey Fisher"],
# ["Daniela Schneider", "Benny Schumm"]]]
Upvotes: 5