T
Ted Zlatanov
Here's a (very simple) function to give you the unique items from a
list you pass:
sub uniques
{
my %unique = ();
$unique{$_}++ foreach @_;
return keys %unique;
}
Now use it like this:
my @columnarray = ( [1,2,3], [1,2,3], [4,5,6], [7,8,9], );
foreach my $column (1 .. scalar @{$columnarray[0]})
{
print "Unique elements in column $column: ";
print join ', ',
uniques(map { $_->[$column-1] }
@columnarray
);
print "\n";
}
I formatted this to be easy to understand, and I tested it with the
data above under
use warnings;
use strict;
and it worked correctly. Please learn from the code posted above - it
shows many useful techniques.
Ted - this is excellent stuff - how exactly can I capture an example of
2 elements representing a duplicate in a variable from this code ???
If you want to remember duplicates (untested):
sub uniques
{
my %unique = ();
$unique{$_}++ foreach @_;
my @ukeys = keys %unique;
my @dkeys = grep { $unique{$_} > 1 } @ukeys;
return [\@ukeys, \@dkeys];
}
then later use it like this in the loop over @columnarray:
my $keys = uniques(map { $_->[$column-1] } @columnarray;
print "Unique elements in column $column: ";
print join ', ', @{$keys->[0]};
print "Duplicate elements in column $column: ";
print join ', ', @{$keys->[1]};
So this modifies uniques() to return two arrays, the list of unique
values and the list of unique values that appeared more than once,
inside a two-element array. The result is stored in $keys. The
original version just returned a list of unique values.
@{$keys->[0]} means "get the first element of the $keys array
reference, and since it's an array itself, convert it to a list
(dereference it)."
Note this is untested
Ted