foreach my $ref ( sort { substr($a, 1) <=> substr($b, 1) } grep { /^A/ } @array ) { ... do whatever ... }