Created
July 23, 2012 14:05
-
-
Save handlename/3163788 to your computer and use it in GitHub Desktop.
intersection for multiple lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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