my %table; # The number of items of group $d of length N (where N is 1 to a # max. length determined elsewhere) found in $b. for my $file (keys %a) { for my $group (keys %{$a{$file}}) { for my $subgroup (keys %{$a{$file}{$group}}) { for my $length (@{$a{$file}{$group}{$subgroup}}) { $table{$file}{$subgroup}[$length]++; } } } } #### my $file = 'xxx'; for my $length (1..$max_length) { for my $unique_d (keys %{$a{$file}{$group}}) { # printing stuff using $table{$file}{$unique_d}[$length] } }