in reply to Can this code be optimized further?

I don't think regex is needed here, and you really should make the code somewhat more forgiving to data variations. What if you use all letters of the alphabet instead of just a and b?
use strict; use warnings; my @temp=("a_1","b_1","a_2","a_3","a_4","b_2","a_5","b_3","a_6","b_4") +; my ($key, $val, %hash); for (@temp) { ($key, $val) = split '_'; if (exists($hash{$key})) { push @{$hash{$key}}, $val; } else { $hash{$key} = [$val]; } } for (sort keys %hash) { print "$_: " . join(' ', @{$hash{$_}}) . "\n"; }
You could probably optimize this further by allocating the entire expected space for each array when initially created, but I'll leave that for the next guy.