On 8/23/06, Derek B. Smith <[EMAIL PROTECTED]> wrote:
From the previous emails, I do not understand what
parts of this code is doing and why is this practical?
The part is $union{$e} = 1 and $isect{$e} = 1 .
Also %count is never used.
It seems like Andrej made some mistakes in his code. I think he could
try something like this:
=item B<set_op>
($union, $isect) = set_op([EMAIL PROTECTED], [EMAIL PROTECTED]);
Computes the union and intersection of two arrays
of strings C<@a> and C<@b>, returning array refs
=cut
sub unisect {
my $a = shift;
my $b = shift;
my %c;
my %h;
%h = (); foreach (@$a) { $c{$_}++ unless $h{$_}++ };
%h = (); foreach (@$b) { $c{$_}++ unless $h{$_}++ };
# $c{$_}++ foreach @$a;
# $c{$_}++ foreach @$b;
my (@union, @isect);
while (my ($k, $v) = each %c) {
push @union, $k;
push @isect, $k if $v>1;
}
return ([EMAIL PROTECTED], [EMAIL PROTECTED]);
}
The reason I needed
%h = (); foreach (@$a) { $c{$_}++ unless $h{$_}++ };
is to prevent the case where the arrays may have duplicates. If they
are known to have distinct elements, this may be replaced by
$c{$_}++ foreach @$a;
And then I suggest Math::Combinatorics to generate each pair of arrays
in a loop like
use Math::Combinatorics;
my $c = Math::Combinatorics->new(count => 2, data => [EMAIL PROTECTED]);
while (@pair = $c->next_combination) {
my ($a, $b) = @pair;
my ($u, $i) = unisect(@pair);
print "(@$a) U (@$b) = (@$u)\n(@$a) A (@$b) = (@$i)\n\n";
}
Kind regards,
Adriano Ferreira.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>