Created
September 6, 2013 23:34
-
-
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!
This file contains hidden or 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/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