use strict; use warnings; use Data::Dumper; ########################################## my (%hist1, %hist2, %hist3); my @required_keys; while () { chomp; my @element = split; my $col0= shift @element; if ($col0 == 1){ $hist1{$_}++ for @element; } elsif ($col0 == 0){ $hist2{$_}++ for @element; } elsif ($col0 == 5){ $hist3{$_}++ for @element; } else { #do stuff here when all else fails, undef/NaNs print "WTF \n"; } }; print Dumper \%hist1; # using your __DATA__ section, not repeated here for brevity