@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))}; }