my %count; while (...) { my($field1, $field2) = split(...); # assume that we already has the key $field1 in %count # so the value would be $count{$field1}. # # assume it to be array reference, pretend you did: # $count{$field1} = [] # # the push() function requires real array so you need # to dereference it first, like this @{$count{$field1}}, # then push the new value # push @{$count{$field1}}, $field2; } #### my %count; while (...) { my($field1, $field2) = split(...); if (exists $count{$field1}) { # force the value to be array ref if it's # not done so $count{$field1} = [$count{$field1}] unless ref $count{$field1}; push @{$count{$field1}}, $field2; } else { $count{$field1} = $field2; # normal assignment } } #### print Dumper(\%count);