in reply to intersection of N arrays
use strict; our %ids; sub intersect { my %count; my @ret; my $count = scalar keys %ids; foreach my $key (keys %ids) { my %this; for (@{$ids{$key}}) { next if $this{$_}; $count{$_}++; $this{$_}++; if ($count{$_} == $count) { push @ret, $_; } } } undef %ids; return @ret; } $ids{'foo'} = [ 3, 4, 4, 5 ]; $ids{'bar'} = [ 3 ]; $ids{'zoo'} = [ 3, 4, 5 ]; print "[ ",join(', ',intersect)," ]\n"; # prints "[ 3 ]"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: intersection of N arrays
by Solo (Deacon) on Jul 01, 2004 at 13:29 UTC | |
Re^2: intersection of N arrays
by Anonymous Monk on Aug 24, 2020 at 02:55 UTC | |
by AnomalousMonk (Archbishop) on Aug 24, 2020 at 04:32 UTC |