my %sorted; foreach my $item (@array) { my ($prefix) = $item =~ /^(\w+)/; push @{$sorted{$prefix}}, $item; } #### foreach my $prefix (keys %sorted) { my @items = @{$sorted{$prefix}}; # do something with @items... }