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


In reply to Weighted Calculation by dirtdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.