Skip to content

Instantly share code, notes, and snippets.

@darynnakhuda
Created September 6, 2013 23:34
Show Gist options
  • Select an option

  • Save darynnakhuda/6471364 to your computer and use it in GitHub Desktop.

Select an option

Save darynnakhuda/6471364 to your computer and use it in GitHub Desktop.
proving I can still write Perl code after all these years (no googling or stack overflowing allowed). Ugly, but it works!
#!/usr/bin/perl
@list1 = (1,2,3,4,5,6);
@list2 = (3,4,5,10);
intersection(\@list1, \@list2);
sub intersection {
my ($a,$b) = @_;
my %intersect = ();
for (@{$a}) {
$intersect{$_} = 1;
}
for (@{$b}) {
if ($intersect{$_} == 1) {
$intersect{$_} += 1;
}
}
@results = ();
@keys = keys(%intersect);
for (@keys) {
if ($intersect{$_} > 1) {
push(@results, $_);
}
}
print "intersection of " .
join(',',@{$a}) . ' and ' . join(',',@{$b}) .
' is ' . join(',', @results) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment