Skip to content

Instantly share code, notes, and snippets.

@handlename
Created July 23, 2012 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handlename/3163788 to your computer and use it in GitHub Desktop.
Save handlename/3163788 to your computer and use it in GitHub Desktop.
intersection for multiple lists
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
sub intersect {
my @lists = grep { scalar @$_ } @_;
my $list_num = scalar @lists;
my %in_list;
for my $list ( @lists ) {
map { $in_list{$_}++ } @$list;
}
return [ grep { $in_list{$_} == $list_num } keys %in_list ];
}
my $list1 = [ 0, 1, 2, 3 ];
my $list2 = [ 1, 3, 5, 7 ];
my $list3 = [ 0, 3, 6, 9 ];
say Dumper intersect( $list1, $list2, $list3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment