while(<>) { ($a, $b, $c, $d) =~ /(...)(...)(...)(...)/; $hash{$a}{$b}{$c}{$d}++; } #### foreach $key (keys %hash) { foreach $key1 (keys %{$hash{$key}}) { foreach $key2 (keys %{$hash{$key}{$key1}}) { # do something } } }