Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Printing and Removing Duplicates in Arrays

by Basilides (Friar)
on Aug 21, 2002 at 12:38 UTC ( [id://191709]=note: print w/replies, xml ) Need Help??


in reply to Printing and Removing Duplicates in Arrays

Similar to thor's solution above--no greps--but this one'll also wipe out the dupes from your arrays:
use strict; my @a = (0,1,2,3,4,5,6,7,8,9); my @b = (0,1,4); my @isect = (); my @diff = (); my %count = (); foreach my $e (@a) { $count{$e} = 1; } foreach my $e (@b) { $count{$e} += 2; } @a = @b = (); foreach my $e (keys %count) { if ($count{$e} == 1) {push @a, $e;} elsif ($count{$e} == 2) {push @b, $e;} else {print "$e ";} }
For a bunch of good examples of this kind of stuff, try the Perl Cookbook, recipes 4.6 & 4.8.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://191709]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found