阵列推送的问题

时间:2017-04-20 21:27:52

标签: perl

我在代码中使用了两个哈希值。一,

Dumper \%userJobCount

$VAR1 = {
          'hina' => 2,
          'maccetta' => 1,
          'vineethk' => 1,
          'jpriyank' => 27,
          'sanchars' => 1,
          'kamran' => 8,
          'wilt' => 7,
          'sakir' => 5,
          'rjernigan' => 8,
          'emichael' => 1,
          'ranjith' => 8,
          'wgutknec' => 7,
          'danchuy' => 1,
          'saurabh4' => 1,
          'chengc' => 9,
          'revathi' => 2,
          'zumach' => 7,
          'hual' => 7,
          'lkashyap' => 2,
          'raviteja' => 17,
          'bsheetal' => 4,
          'horgan' => 2,
          'tongz' => 6,
          'demat' => 1,
          'matthew6' => 14,
          'alward' => 1,
          'adalton' => 1,
          'sydneyw' => 5,
          'yashodhc' => 1,
          'makam' => 1,
          'surajs' => 9,
          'radish' => 2,
          'sudiptac' => 2,
          'adityak' => 4,
          'dodgson' => 4,
          'sudipp' => 6,
          'zaw' => 1,
          'umeshr' => 23,
          'zukas' => 6
        };

%userJobSubtest = (
        name      => '',
        username  => '', 
        rc        => PASS,
        notes     => ''
    );

这是我使用它们的代码:

foreach my $key (keys %userJobCount) {
    if( $userJobCount{$key} > $jobLimit) {
        %userJobSubtest = (
            name      => $key,
            username  => $key, 
            rc        => WARN,
            notes     => ''
        );
        #print Dumper \%userJobSubtest;
        push(@{$rtn{subtests}}, \%userJobSubtest);
    }
}
print Dumper %rtn;

我只是显示总输出中的子测试键值:

$VAR6 = [
          {
            'rc' => 3,
            'notes' => '',
            'name' => 'umeshr',
            'username' => 'umeshr'
          },
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0],
          $VAR6->[0]
        ];

在代码中,这部分被注释掉了:

#print Dumper \%userJobSubtest;

当我运行它时,它具有其值的值大于$ jobLimit(= 5)的其他键的值 有没有人知道推送出了什么问题?当我推动的内容实际上不是同一个值时,为什么我会得到重复的条目?

1 个答案:

答案 0 :(得分:3)

您使@{ $rtn{subtests} }的每个元素都成为相同哈希的引用。

修正:

my %userJobSubtest = (
    name      => $key,
    username  => $key, 
    rc        => WARN,
    notes     => ''
);

push(@{$rtn{subtests}}, \%userJobSubtest);

my创建一个变量。始终使用use strict; use warnings qw( all ); !!!