in reply to Proportionate redistribution of weights
my @metrics = ( # Weights don't need to add up to anything specific. [ 'Leadership', 0.40 ], [ 'Punctuality', 0.20 ], [ 'Cleanliness', 0.30 ], [ 'Responsiveness', 0.10 ], ); my @scores = (2, undef, 2, 2); my $total_weight = 0; foreach (0..$#metrics) { $total_weight += $metrics[$_][1] if (defined($scores[$_])); } my $total = 0; foreach (0..$#metrics) { if (defined($scores[$_])) { my $metric = $metrics[$_][0]; my $score = $scores[$_]; my $weight = $metrics[$_][1] / $total_weight; my $weighted = $score * $weight; $total += $weighted; printf("%-20s %d %5.3f %5.3f$/", $metric, $score, $weight, $weig +hted); } else { my $metric = $metrics[$_][0]; printf("%-20s - ----- -----$/", $metric); } } printf("%-20s - ----- %5.3f$/", 'Total', $total); output ====== Leadership 2 0.500 1.000 Punctuality - ----- ----- Cleanliness 2 0.375 0.750 Responsiveness 2 0.125 0.250 Total - ----- 2.000
|
|---|