Help for this page

Select Code to Download


  1. or download this
    my %in_subset = map { $_ => 1 } @subset; # Set the value 1 for each ke
    +y in @subset
    
  2. or download this
    my @filtered = grep { exists $in_subset{$_} } @array; # grep for only 
    +the elements of @array which are in subset
    my @same = grep { $in_subset{$_} } @array; # Technically also works be
    +cause trying to access a non existing key will return undef, which is
    + false
    #Edited (missing } on the second line) thanks to AnomalousMonk
    
  3. or download this
    my %index_map = map { $array[$_] => $_  } 0..$#array; # Associate each
    + element with its index (only works for unique values though)
    my @matching_indexes = map { $index_map{$_} } @subset; # Get the index
    + for each value in @subset