my @array1 = (1,2,3,4,5); my @array2 = (4,5,6,7,8); my %counts; my @union = grep ++$counts{$_} == 1, @array1, @array2; my @intersection = grep $counts{$_} == 2, @array1; my @difference = grep $counts{$_} == 1, @array1; my @intersection_c = grep $counts{$_} == 1, @array1, @array2; print "Union: @union\n"; print "Intersection: @intersection\n"; print "Difference: @difference\n"; print "Complement of intersection: @intersection_c\n";
Union: 1 2 3 4 5 6 7 8 Intersection: 4 5 Difference: 1 2 3 Complement of intersection: 1 2 3 6 7 8
Note that your code (and almut's) incorrectly calculates the difference.
In reply to Re: tweaking of perldoc function (sorting/preserving order)
by ikegami
in thread tweaking of perldoc function (sorting/preserving order)
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |