use Data::Dumper; @array = ( "APPLE", "ORANGE", "PEACH", # Use this for the ORDER "GRAPE", "BANANA", "PINEAPPLE" ); @subset = ( "PINEAPPLE", "GRAPE", "APPLE" ); # A required subset but NOT # in the required order @results = (); # The subset with its elements in the # same order as the 'reference' array # ---- Method 1: Using arrays directly @tmp = (); foreach my $check (@subset) { my ($index) = grep { $array[$_] eq $check } 0..$#array; push(@tmp, $index); } @index_list = sort {$a <=> $b} @tmp; # Sort the index values numerically @results = @array[@index_list]; # Slice the index items out of '@array' printf("Method 1:\n"); print Dumper(@results); printf("\n"); # ---- Method 2: Using a sub Reset_Order(\@subset, \@array, \@results); printf("Method 2:\n"); print Dumper(@results); printf("\n"); # ========== end mainline ========== # ------------------------------------- # Reset_Order - ... # Uses Globals: # ------------------------------------- sub Reset_Order { my ($small_ref, $large_ref, $res_ref) = @_; my (@tmp); @tmp = (); foreach my $check (@$small_ref) { my ($index) = grep { $$large_ref[$_] eq $check } 0..$#$large_ref; push(@tmp, $index); } my @index_list = sort {$a <=> $b} @tmp; # Sort the index values numerically @$res_ref = @$large_ref[@index_list]; # Slice the index items out of the large array return(1); } # end Reset_Order