#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = ("world", "today", "is nice"); my %hash = ( 0 => "Hello", 1 => "world", 2 => "today", 3 => "is" , 4 => "nice" ); my @hashValues = values %hash; print Dumper \@hashValues; use List::Compare; my $lc = List::Compare->new(\@hashValues, \@array); my @intersection = $lc->get_intersection; my @union = $lc->get_union; print Dumper \@intersection; __END__ Monks$ perl test.pl $VAR1 = [ 'nice', 'Hello', 'today', 'world', 'is' ]; $VAR1 = [ 'today', 'world' ]; #### my @new_array; while (my ($key, $value) = each %hash) { for (@array) {push @new_array, $key if ($value eq $_);} } __END__ $VAR1 = [ '2', '1' ]; #### my @new_array_sorted = sort @new_array; print Dumper \@new_array_sorted; __END__ $VAR1 = [ '1', '2' ]; #### my @new_array; while (my ($key, $value) = each %hash) { push @new_array, $key if (grep { /^$value$/ } @array); } __END__ $VAR1 = [ '2', '1' ]; #### my @new_array; while (my ($key, $value) = each %hash) { map { if($value eq $_) { push @new_array, $key } } @array; } __END__ $VAR1 = [ '2', '1' ]; #### no if $] >= 5.018, "experimental::smartmatch"; #disable warnings while (my ($key, $value) = each %hash) { push @new_array, $key if ($value ~~ @array); } __END__ $VAR1 = [ '2', '1' ]; #### #!/usr/bin/perl use strict; use warnings; use Benchmark qw( timethese cmpthese ) ; no if $] >= 5.018, warnings => "experimental::smartmatch"; my @array = ("world", "today", "is nice"); my %hash = ( 0 => "Hello", 1 => "world", 2 => "today", 3 => "is" , 4 => "nice" ); my @new_array; my $results = timethese(1000000000, { 'map' => benchmarkMap(), 'grep' => benchmarkGrep(), 'foreach' => benchmarkForeach(), 'smartmatch' => benchmarkSmartMatch(), }, 'none'); cmpthese( $results ) ; sub benchmarkSmartMatch { while (my ($key, $value) = each %hash) { push @new_array, $key if ($value ~~ @array); } } sub benchmarkGrep { while (my ($key, $value) = each %hash) { push @new_array, $key if (grep { /^$value$/ } @array); } } sub benchmarkForeach { while (my ($key, $value) = each %hash) { for (@array) {push @new_array, $key if ($value eq $_);} } } sub benchmarkMap { while (my ($key, $value) = each %hash) { map { if($value eq $_) { push @new_array, $key } } @array; } } __END__ Monks$ perl test.pl Rate foreach smartmatch grep map foreach 76569678/s -- -30% -59% -64% smartmatch 109170306/s 43% -- -42% -49% grep 187969925/s 145% 72% -- -13% map 215053763/s 181% 97% 14% --