Oh, heh. Delete non-duplicates. Right, sorry. :)
Anyway, what you have posted works fine (which is what I thought initially, before I got confused about which you were wanting to remove).
[johnsca@CORY tmp]$ cat tst.pl
#!/usr/bin/perl
my @Tapes = (
['Label 1', 'p', 'l', 'P'],
['Label 1', 'p', 'l', 'P'], # Duplicate
['Label 2', 'p', 'l', 'P'],
['Label 3', 'p', 'l', 'P'],
);
hash_tapes(@Tapes);
sub hash_tapes (@) {
my @tapes = @_;
my %Tape_Cat =();
foreach my $tape (@tapes) {
my ( $label, $protection, $location, $pool ) = @{$tape};
push @{ $Tape_Cat{$label} }, [ $protection, $location, $pool ]
+;
}
# Get rid of non-duplicates
my $count = 0;
foreach my $tape (@tapes) {
my ($label,$protection,$location,$pool) = @{$tape};
if ( @{$Tape_Cat{ $label}} < 2 ) {
print "Deleting ". $label.
"Because it has ".@{$Tape_Cat{ $label }}.
" record(s)\n";
$count++;
delete $Tape_Cat{$label}
}
}
my @keys = keys %Tape_Cat;
print "Deleted $count keys, keys: @keys\n";
return \%Tape_Cat;
}
[johnsca@CORY tmp]$ ./tst.pl
Deleting Label 2Because it has 1 record(s)
Deleting Label 3Because it has 1 record(s)
Deleted 2 keys, keys: Label 1
Correct, no? You should probably double-check the data that this function is actually getting. Use Data::Dumper at the begining of the function, or the built-in perl debugger. Update: If the data is coming from an outside source, check for strange things like leading or trailing newlines or linebreaks, or non-printable characters, or such.
Good luck. :)
bbfu
Black flowers blossum
Fearless on my breath |