foreach (@animals) { $animals{$_}++; } # initialize %animals hash # could be $animals{$_}++ for @animals;