in reply to How to Order an Array's Elements to Match Another Array's Element Order
OUTPUT:#!/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";
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';
|
|---|