in reply to Weighted Calculation
And this prints the following output:#!/usr/bin/env perl use strict; use warnings; my %user_weight = (DREW => .5, TIM => 2,); my ($tot_users, $tot_non_weighted, $tot_events); while (<DATA>) { # Stuff data into Hash and get count of total events and users my ($user, $val) = split; $tot_events += $val; $tot_users++; $tot_non_weighted ++ unless exists $user_weight{$user}; } my $number_of_weighed_parts; $number_of_weighed_parts += $_ for values %user_weight; my $indiv_part = $tot_events / ($tot_non_weighted + $number_of_weighed +_parts); print "The $tot_non_weighted non_weighted workers get each $indiv_part +\n"; for my $weighted_user (keys %user_weight) { print "$weighted_user gets ", $indiv_part * $user_weight{$weighted +_user}, "\n"; } __DATA__ TIM 150 JOE 124 JACK 111 KATE 145 DREW 177
which seems correct (at least if I understood correctly what you are trying to do). You would probably want to add some rounding, but I leaves that to you.$ perl weight.pl The 3 non_weighted workers get each 128.545454545455 DREW gets 64.2727272727273 TIM gets 257.090909090909
Edit 15:41 UTC: corrected a bug in the above code and the output.
Edit2 16:40 UTC: I had not seen moritz's solution when I posted my messages above. My proposed solution (be it in English or in Perl code) is to a large extent equivalent, sorry for posting almost the same thing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Weighted Calculation
by dirtdog (Monk) on May 01, 2014 at 15:50 UTC | |
by Laurent_R (Canon) on May 01, 2014 at 15:59 UTC | |
by dirtdog (Monk) on May 01, 2014 at 16:30 UTC |