Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: comparing two array

by si_lence (Deacon)
on Jun 17, 2009 at 12:36 UTC ( #772372=note: print w/replies, xml ) Need Help??

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.

use 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";


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772372]
[stevieb]: my RPi::WiringPi now fully supports: GPIO, LCDs, digital potentiometers, analog to digital converters, shift registers, barometric pressure sensor, direct access to SPI bus, and ability for more :)
[stevieb]: ...just need to perform many tests and POD review this week, then I'm going to put together some real projects and post about them

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-01-22 22:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.