dirtdog has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I'm having a very hard time with the execution of weighting. In my code, I have multiple users, but only 2 of them (in this case) have a weighting. Drew, should get half (.5) of the work of the other non-weighted users...And Tim should get double the work. I'm not getting my desired result. Tim ends up getting a lot more than double the work.
#!/usr/bin/env perl my %user_weight = (DREW => .5, TIM => 2,); my $roundup; my $tot_per_user; my $tot_events; my $tot_users; undef %userMax4type; my @tmp; my $tmp=0; my $weight_tot=0; my %USERS; while (<DATA>) { # Stuff data into Hash and get count of total events and users my $aref = [split]; $USERS{$aref->[0]} = $aref->[1]; $tot_events += $aref->[1]; $tot_users++; } foreach ( keys %USERS ) { #check if a User has an assigned weight and perform calculatio +n if ( exists($user_weight{$_}) ) { $tot_per_user = $tot_events/$tot_users * $user_weight{ +$_}; $roundup=sprintf("%.0f", $tot_per_user); print "roundup for $_ is: $roundup\n"; $weight_tot += $roundup; } else { #keep track of those users who do not have a weighting push @tmp, $_; $tmp++; } } #perform calculation on non-weighted users foreach (@tmp) { $tot_per_user = ($tot_events-$weight_tot)/$tmp * 1; $roundup=sprintf("%.0f", $tot_per_user); print "roundup for $_ is: $roundup\n"; } __DATA__ TIM 150 JOE 124 JACK 111 KATE 145 DREW 177
Does anyone know of a better way to redistribute work based on a weighting system
any help is much appreciated
thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weighted Calculation
by moritz (Cardinal) on May 01, 2014 at 14:50 UTC | |
by dirtdog (Monk) on May 01, 2014 at 16:35 UTC | |
by Laurent_R (Canon) on May 01, 2014 at 16:39 UTC | |
by moritz (Cardinal) on May 01, 2014 at 17:00 UTC | |
by tye (Sage) on May 01, 2014 at 18:19 UTC | |
| |
by Laurent_R (Canon) on May 01, 2014 at 17:17 UTC | |
| |
by 2teez (Vicar) on May 01, 2014 at 19:14 UTC | |
by AnomalousMonk (Archbishop) on May 02, 2014 at 00:21 UTC | |
|
Re: Weighted Calculation
by Laurent_R (Canon) on May 01, 2014 at 15:31 UTC | |
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 | |
|
Re: Weighted Calculation
by Laurent_R (Canon) on May 01, 2014 at 15:05 UTC |