Perl哈希脚本

时间:2012-10-17 06:21:03

标签: perl

我在Perl中的问题是这样的:

从标准输入,一组perl线读取一系列员工编号和每日工作时间。员工编号和工作小时数应以空格分隔。使用哈希值计算总工作小时数和每个工作时段的平均小时数。按分类员工编号,工作期数,工作总时数以及每个工作期间的平均小时数打印报告。假设某些员工是兼职时间表,并且与正式员工的工作天数或工作时间不同。

我的脚本是:

#!/usr/bin/perl
use strict;
use warnings;

my @series = qw(41234 9 67845 8 32543 10 84395 7 57543 9 23545 11);
my $workper = 3;
my %empwork;

while (my $series = shift @series) {

    my $nums = shift @series;
    $empwork{$series} += $nums;
}

my $tot;
foreach (sort keys %empwork) {

    $tot += $empwork{$_};
}

my $avg = $tot/$workper;
print "Sorted Employee Numbers:\n";
foreach my $empnum(sort keys %empwork) {

    print "$empnum\n";
}
print "The number of work periods is $workper\n";
print "Total number of hours is $tot\n";
print "Average number of hours per work period is $avg\n";

我的输出是:

Sorted Employee Numbers:
23545
32543
41234
57543
67845
84395
The number of work periods is 3
Total number of hours is 54
Average number of hours per work period is 18

任何人都可以告诉我脚本中是否有任何错误。如果是,请帮忙。提前谢谢。

如果我像这样使用循环%empwork:

foreach my $empnum(sort keys %empwork) {

    $tot += $empwork{$_};
    print "$empnum\n";
}

然后我会得到输出:

Sorted Employee Numbers:
23545
32543
41234
57543
67845
84395
The number of work periods is 3
Total number of hours is 0
Average number of hours per work period is 0
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.
Use of uninitialized value in hash element at /tmp/135043087931085.pl line 16.
Use of uninitialized value in addition (+) at /tmp/135043087931085.pl line 16.

我尝试了如下程序。但它不起作用。

#!/usr/bin/perl
use strict;
use warnings;
my @series = qw(41234 9 67845 8 32543 10 84395 7 57543 9 23545 11 23545 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";

我哪里错了?

1 个答案:

答案 0 :(得分:2)

这段代码存在问题:

foreach my $empnum(sort keys %empwork) {

    $tot += $empwork{$_};
    print "$empnum\n";
}

您正在使用$empnum作为循环迭代器变量,但随后引用$empwork{$_}。这就是你得到错误的原因。只需将其替换为$empwork{$empnum},您就可以了。

上面显示的其余代码运行正常。但是,一些建议:

源阵列中是否存在重复的员工编号?示例数据未显示任何内容。如果没有重复项,您只需执行此操作即可填充哈希值,并取消while循环:

%empwork = @series;

此外,在这部分:

foreach (sort keys %empwork) {

    $tot += $empwork{$_};
}

当您没有做依赖于订单的事情时,没有理由对密钥进行排序。它只是让翻译做了不必要的工作。在这种情况下,您甚至不需要钥匙;你只对增加价值感兴趣。所以,你可以做到这一点,效率更高:

foreach (values %empwork)
{
    $tot += $_;
}

(当然,你可以改为组合两个循环)。

更新:以下是我认为可以满足您所有要求的完整更正代码。

#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/sum/;

my @series = qw(41234 9 67845 8 32543 10 84395 7 57543 9 23545 11 23545 1 23545 2 23545 7);
my $total_periods = 0;
my $total_hours = 0;
my %empwork;
while (my $series = shift @series) {
    #For each employee, save a list of the number of times they worked
    push @{$empwork{$series}}, shift @series;
}

print "Sorted Employee Numbers:\n";
foreach my $empnum(sort keys %empwork) {
    my $periods = @{ $empwork{$empnum} };
    my $hours   = sum(@{ $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";
相关问题