in reply to Find all values of an AoA by columns
The traversal is easy as already shown. The next step is to build a list of counts. Consider:
use strict; use warnings; use Data::Dump::Streamer; my @AoA = ( [ "X", "Z", "W", "X", "Z", "Z" ], [ "Z", "Z", "X", "X", "X", "W" ], [ "X", "Z", "X", "W", "W", "W" ], [ "Z", "X", "W", "X", "Z", "Z" ], [ "Z", "X", "W", "X", "X", "W" ], [ "Z", "X", "X", "X", "Z", "W" ], ); my @sums; for my $row (0 .. $#AoA) { for my $col (0 .. $#{$AoA[$row]}) { ++${$sums[$col]}{$AoA[$row][$col]}; ++${$sums[$col]}{total}; } } Dump \@sums;
Prints:
$ARRAY1 = [ { total => 6, X => 2, Z => 4 }, { total => 6, X => 3, Z => 3 }, { total => 6, W => 3, X => 3 }, { total => 6, W => 1, X => 5 }, { total => 6, W => 1, X => 2, Z => 3 }, { total => 6, W => 4, Z => 2 } ];
'total' isn't required if there are no missing rows, but may be a good sanity check. The remainder of the solution is left as an exercise for the reader.
BTW: you can't have a tie with the threshold at 60%, it must be 50% or less. Indeed your sumple supports 50% or there wouldn't be a Z for the penultimate column.
|
|---|