in reply to Find unique elements from multiple arrays
# your arrays my @a = qw(a c d); my @b = qw(s d v); my @c = qw(l d s); my @d = qw(i c f); # the hash we'll use to preserve uniqueness my %unique = (); # for every character in each of the defined arrays for my $char (@a, @b, @c, @d) { # whenever we run into a character, we set its # corresponding value to 1 in the unique hash. Even if we # run into the same character more than once, it'll only have # a single entry in our hash, as is the nature of hashes. $unique{$char} = 1; } # now, the keys in the %unique hash should be the unique # values from those arrays. We'll sort the keys for good # measure my @unique = sort keys %unique; # @unique is now the unique elements of those arrays. Kudos.
Hashes are your friend (I use them a LOT when looking for duplicates in company data files). Learn to use them wisely and you'll appreciate perl's power and simplicity a lot sooner.
Or, for a quickie:
my %unique = map { $_=>1 } @a, @b, @c, @d; my @unique = sort keys %unique;
--
perl: code of the samurai
|
|---|