1

I have two hashes which I use in my code. One,

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
        };

and

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

Here's the code where I use them:

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;

I'll just show the subtests key value from the total output:

$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]
        ];

In the code, this part is commented out:

#print Dumper \%userJobSubtest;

When I run this, it has the values of other keys whose values are more than the $jobLimit (= 5) Does anyone have an idea what's going wrong with the push? Why am I getting duplicate entries when what I am pushing shouldn't actually be the same value?

1
  • PS - You'll get better results if you pass references to hashes and arrays to Dumper. For example, print Dumper %rtn; should be print Dumper \%rtn;. In this case, print Dumper $rtn{subtests};` would be even better. Commented Apr 20, 2017 at 21:33

1 Answer 1

3

You made every element of @{ $rtn{subtests} } a reference to the same hash.

Fix:

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

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

The my creates a variable. Always use use strict; use warnings qw( all );!!!

Sign up to request clarification or add additional context in comments.

6 Comments

That worked. The code had use strict; use warnings; What's the difference introduced with use warnings qw (all); ?
That means you were also clobbering some other hash, and that you now have two variables with the same name in scope. Rename one of them.
I didn't understand one thing. The reference to \%userJobSubtest should point to %userJobSubtest. So, when the value of %userJobSubtest changes, shouldn't the reference also point to the new value? I'm sorry, I get confused with references.
Re "shouldn't the reference also point to the new value?", Yes, and it did. That's the problem you asked us to fix. By replacing the single %userJobSubtest with a new one created each pass of the loop, the problem goes away.
That's right and it got solved because I re-declared the hash using my. What I meant to ask is why is the my important here? Without using the my, why isn't the reference pointing to the new value after I changed the value of the hash? Why does it still point to the old value?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.