in reply to Perl Hash Script

prithviraj:

What output do you get, and what were you expecting?

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Perl Hash Script
by prithviraj (Novice) on Jan 08, 2013 at 16:06 UTC
    I get the output as:
    Sorted Employee Numbers: 23545 1 periods 20 hours 20 average 32543 1 periods 10 hours 10 average 41234 1 periods 9 hours 9 average 57543 1 periods 9 hours 9 average 67845 1 periods 8 hours 8 average 84395 1 periods 6 hours 6 average The number of work periods is 6 Total number of hours is 63 Average number of hours per work period is 10.5
    But the output I am expecting is:
    Sorted Employee Numbers: 23545 1 periods 20 hours 20 average 32543 1 periods 10 hours 10 average 41234 1 periods 9 hours 9 average 57543 1 periods 9 hours 9 average 67845 1 periods 8 hours 8 average 84395 1 periods 6 hours 6 average The number of work periods is 9 Total number of hours is 63 Average number of hours per work period is 7
    Please help.

      prithviraj:

      The only difference I see is the totals at the end. If that's the case, you should probably accumulate the totals in the first loop.

      However, I rather suspect that you really don't want that. Your data shows multiple work periods for at least one employee, yet your output doesn't indicate it. I'm thinking that what you *really* want to do is to track both the number of periods *and* the total time for each employee. You might try it something like:

      while (my $series = shift @series) { my $nums = shift @series; $empwork{$series}{TTL} += $nums; $empwork{$series}{CNT}++; }

      Then later you can show the number of periods and the total time and compute the average.

      for my $empnum (sort keys %empwork) { ... my $periods = $empwork{$empnum}{CNT}; ... }

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.