http://www.perlmonks.org?node_id=11106282


in reply to How to Order an Array's Elements to Match Another Array's Element Order

Hello, ozboomer,

Your method (same method, but with or without subroutine) is clear and good. But if you have large data, it is too slow, because time complexity of the method is N*M. For every element from subset (M) you check all elements from list (N). For better time complexity you can use hashes (search: perldata).

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @array = ( "APPLE", "ORANGE", "PEACH", # Use this for the O +RDER "GRAPE", "BANANA", "PINEAPPLE" ); my @subset = ( "PINEAPPLE", "GRAPE", "ORANGE", "APPLE" ); # A requir +ed subset but NOT # in the required order my @results = (); # The subset with it +s elements in the # same order as the 're +ference' array # ---- Method 2: Using hash my %indexes; my $index = 0; map { $indexes{ $_ } = $index; $index ++; } @array; printf " Index of element '$_' is: %d\n", $indexes{ $_ } for @subset; my @tmp = (); foreach my $check ( @subset ){ my( $index ) = $indexes{ $check }; push( @tmp, $index ); } my @index_list = sort {$a <=> $b} @tmp; # Sort the index values numer +ically @results = @array[@index_list]; # Slice the index items out of ' +@array' print "Method 2 (uses hash):\n"; print Dumper(@results); print "\n";
OUTPUT:
Index of element 'PINEAPPLE' is: 5 Index of element 'GRAPE' is: 3 Index of element 'ORANGE' is: 1 Index of element 'APPLE' is: 0 Method 2 (uses hash): $VAR1 = 'APPLE'; $VAR2 = 'ORANGE'; $VAR3 = 'GRAPE'; $VAR4 = 'PINEAPPLE';