in reply to Returning an array the way it was written

As stated in my reply to your previous, similar question the percent sign (here in %names) indicates a hash, not an array and does not retain a natural order. Use an array, not a hash.

Here's an SSCCE to demonstrate:

#!/usr/bin/env perl use strict; use warnings; my @names = qw/XX AA BC DD/; print "@names\n";

Further note that you are not returning anything here, merely outputting it.