You could use a hash. Use values from the first array as keys to values taken from the second array. Then just do a foreach my $key ( sort keys %the_hash) { do stuff }
. If the key values are not unique then using a hash of arrays and pushing the values into the hash works.
#! perl
use strict;
use warnings;
my @key_data = ('1', '6', '8', '4', '5', '4', '5');
my @val_data = ('a', 'c', 'd', 'f', 'w', 'z', 'w');
my %the_hash;
for ( my $ii=0; $ii<=$#key_data; $ii++) {
push @{$the_hash{$key_data[$ii]}}, $val_data[$ii];
}
for my $key ( sort keys %the_hash ) {
print "key $key\n";
foreach my $val ( @{$the_hash{$key}} ) {
print " $val\n";
}
}