in reply to How to Order an Array's Elements to Match Another Array's Element Order
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11106277 use warnings; my @array = ( "APPLE", "ORANGE", "PEACH", # Use this for the O +RDER "GRAPE", "BANANA", "PINEAPPLE" ); my @subset = ( "PINEAPPLE", "GRAPE", "APPLE" ); # A required subset +but NOT # in the required order my %order; @order{ @array } = 1 .. @array; my @results = sort { $order{$a} <=> $order{$b} } @subset; print "results: @results\n";
Outputs:
results: APPLE GRAPE PINEAPPLE
|
|---|