@name = qw(A B C D E); @category = qw(a b c d e); @subcategory = qw(1 2 3 4 5); @indices = sort {$category[$a] cmp $subcategory[$b] or $category[$a] cmp $subcategory[$b]} (0 .. 4); foreach $index (@indices) { print "$category[$index]/$subcategory[$index]: $name[$index]\n"; }