This is probably not the most efficient way to do this but it will work. This uses a hash to keep track of the duplicates.
my %hash;
my @NewArray;
#read all of the values of the array into keys of the hash,
#this eliminates all of the duplicates
foreach my $var (@OriginalArray) {
$hash{$var}=0;
}
#this loop marks any variable that appeared twice in the
#original array.
foreach my $var2 (@OriginalArray) {
$hash{$var}++;
}
#This fills a new array based on the information from the
#above loop
foreach my $var3 (keys (%hash)) {
if (!$hash{$var3} > 1) {
print "$hash{$var3} deleted\n";
} else {
$NewArray[@NewArray]=$hash{$var3};
}
}
@OriginalArray = @NewArray;
update ++
dpuu and
thelenm for their much better and more efficient answers to this question.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.