The question doesn't have an answer, because your initial hash cannot exist. Hash keys have to be unique, otherwise they overwrite each other. So after your initial assignment, %pets looks like this:
%pets = (
'pony' => 2,
'cat' => 2,
'dog' => 2
);
Now, if you had the original list assigned into an array, that would be a different story:
#!/usr/bin/perl -w
use strict;
my @pets = ("dog", 1, "cat", 2, "pony", 2, "dog", 2);
my %pets;
while (@pets) {
my $key = shift @pets;
my $val = shift @pets;
$pets{$key} += $val;
}
foreach (sort keys %pets) {
print "$_: $pets{$_}\n";
}
Hope that helps!