my %hash = map {split} ; for my $key ( map { $_->[0] } sort custom_sort map {[ $_, $hash{$_} =~ /([A-Z]*)(\d+|[A-Z]+)/ig ]} keys %hash) { print "Key = $key, Value = $hash{$key}\n"; } sub custom_sort { my $i = 1; for (;;) { return -1 unless defined $a->[$i]; return 1 unless defined $b->[$i]; my $c = $a->[$i] cmp $b->[$i] || $a->[$i+1] <=> $b->[$i+1]; return $c if $c; $i += 2; } } __DATA__ a 2 b 1 c 12 d a12 e a9 f a2b3 g a2b4 h b2b4 i 12 j a2bb5 k a2bb5a l a2bb5b