in reply to Calculate overlap between 2 ranges of numbers
Please see comments
#!/usr/bin/perl my $val=0; my @array_a = (1..8); my @array_b = (6..10); my %count= (); my @all = (); my @inter =(); my @different =(); foreach $val (@array_a, @array_b) { $count{$val}++; } #build hash # that counts the number of times a number is seen # 1=>1, 2=>1, ... 6=>2, 7=>2, etc... foreach $val (keys %count) { push @all, $val; # the keys have to be unique in hash %count push @{ $count{$val} > 1 ? \@inter : \@different}, $val; # lookup + ternary operator # if the $val of %count is >1 push to @inter. if not, push to @differ +ent }; @all = sort {$a <=> $b} @all; #sort @different = sort {$a <=> $b} @different; @inter = sort {$a <=> $b} @inter; print "all:\n"; foreach (@all) { print "\t$_\n"; }; print "Different:\n"; foreach (@different) { print "\t$_\n"; }; print "Intersect:\n"; foreach (@inter) { print "\t$_\n"; };
HTH, Rob
|
|---|