"be consistent" PerlMonks

### Perl Hash Script

by prithviraj (Novice)
 on Jan 08, 2013 at 15:39 UTC 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";

Replies are listed 'Best First'.
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};

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

```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/

Re: Perl Hash Script
by roboticus (Chancellor) on Jan 08, 2013 at 15:42 UTC

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

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.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012260]
Approved by Corion
help
Chatterbox?
 [Corion]: Maybe I should do the Filter::Simple release on the next weeked - this would give me one more article to milk from this theme [Corion]: Oooh. I should write me a "diff" tool that looks at what modules are published on Github but not on CPAN ;)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-04-24 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (437 votes). Check out past polls.