I have been staring at the following array of arrays for a few hours now and any combination of merge
, select
, find
, map
, flatten
, etc., has not been able to help me yet.
I created some ugly nested loops, but nothing coming close to the Ruby way.
I have an array of arrays:
[
["Ja", nil, "Bijna", nil, "Ja"],
["Nee", nil, "Nee", "Ja", "Nee"],
[nil, nil, "Bijna", "Nee", "Nee"],
["Ja", nil, nil, "Nee", "Ja"],
["Bijna", nil, "Bijna", "Nee", "Ja"]
]
I need to count all the instances of the values vertically, yielding a hash of hashes like this:
{
{"Ja" => 2, "Nee" => 2, => "Bijna" => 1, "nil" => 1},
{"Ja" => 0, "Nee" => 0, => "Bijna" => 0, "nil" => 5},
{"Ja" => 0, "Nee" => 1, => "Bijna" => 3, "nil" => 1},
{"Ja" => 0, "Nee" => 1, => "Bijna" => 3, "nil" => 1},
{"Ja" => 3, "Nee" => 2, => "Bijna" => 0, "nil" => 0}
}
Please note that each horizontal in the hash is the count of the vertical in the array.
Yielding a new array of arrays (of arrays) is also acceptable, although I like the hash better:
[
[["Ja", 2], ["Nee", 2], ["Bijna", 1], ["nil", 1]],
[[etc]]
]