in reply to comparing two array
This is more or less directly from the Perl Cookbook
It passes through the first array remembering all elements in the 'seen' hash, then passes through the second one and only remembers the elements already encountered in the first.
It also has a more idomatic version there if you want is more perlish.
cheersuse strict; use warnings; my @a = (1, 3, 5, 6, 7, 8); my @b = (2, 3, 5, 7, 9); my (@isect, %seen, %isect); foreach my $e (@a) { $seen{$e} = 1 } foreach my $e (@b) { if ( $seen{$e} ) { $isect{$e} = 1 } } @isect = keys %isect; print "@isect\n";
si_lence
|
---|
In Section
Seekers of Perl Wisdom