Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl Hash Script

by prithviraj (Novice)
on Jan 08, 2013 at 15:39 UTC ( #1012260=perlquestion: print w/ replies, xml ) Need Help??
prithviraj has asked for the wisdom of the Perl Monks concerning the following question:

My question in Perl using simple hashes is: Read a series of employee numbers and daily working hours from standard input, one set perl line.The employee number and the hours worked should be separated by a space. Use hashes and calculate the total number of hours worked and the average number of hours per work period. Print out a report by sorted employee number, the number of work periods, the total hours worked, and the average number of hours per work period. Assume that some of the employees are on a part-time schedule and do not work the same number of days or hours as regular employees. My code is:
#!/usr/bin/perl use strict; use warnings; my @series = qw(41234 9 67845 8 32543 10 84395 7 57543 9 23545 11 2354 +5 1 23545 2 23545 6); my $total_periods = 0; my $total_hours = 0; my %empwork; while (my $series = shift @series) { my $nums = shift @series; $empwork{$series} += $nums; } print "Sorted Employee Numbers:\n"; foreach my $empnum(sort keys %empwork) { my $periods=0; $periods++; my $hours = 0; $hours += $empwork{$empnum}; my $avg = $hours/$periods; $total_periods += $periods; $total_hours += $hours; print "$empnum\n$periods periods\n$hours hours\n$avg average\n\n"; } my $grand_avg = $total_hours/$total_periods; print "The number of work periods is $total_periods\n"; print "Total number of hours is $total_hours\n"; print "Average number of hours per work period is $grand_avg\n";
I am not getting the exact output. Where am I going wrong? Please help. Thanks in advance.

Comment on Perl Hash Script
Download Code
Re: Perl Hash Script
by roboticus (Canon) on Jan 08, 2013 at 15:42 UTC

    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.

      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.

Re: Perl Hash Script
by muba (Priest) on Jan 08, 2013 at 15:59 UTC

    One thing I can spot right away regards these lines.

    foreach my $empnum(sort keys %empwork) { my $periods=0; $periods++; my $hours = 0; $hours += $empwork{$empnum};

    Your code boils down to

    foreach my $empnum(sort keys %empwork) { my $periods = 1; my $hours = $empwork{$empnum}
    I doubt that is what you really want.

    Did you mean this instead?

    my $periods=0; my $hours = 0; foreach my $empnum(sort keys %empwork) { $periods++; $hours += $empwork{$empnum};

      I am not getting the exact output for your code Sir.

        I never claimed you it would. I was merely questioning whether your script might suffer from a logical error, and explaining why I think it does. Does it?

        Edit: s/you/it/

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012260]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-09-23 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls