# The @AllPatterns array is probably fed elsewhere, this is just for the test @AllPatterns = ([qw(1 2 1 1 8)], [qw(1 6 5 12 12)], [qw(1 1 1 1 1)], [qw(1 1 1 1 1)]); for $v (@AllPatterns) { ++$h{join(' ',sort {$a <=> $b} @$v)}; } for $k (sort keys %h) { print "$h{$k} - $k\n"; } #### @AllPatterns = ('1 2 1 1 8', '1 6 5 12 12', '1 1 1 1 1', '1 1 1 1 1'); for $v (@AllPatterns) { ++$h{join(' ',sort {$a <=> $b} split(' ',$v))}; }