in reply to Can this code be optimized further?
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.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"; }
|
|---|