There's more than one way to do things PerlMonks

### Re^2: Selective printing of the Duplicates

by Thomas Kennll (Acolyte)
 on Jan 30, 2013 at 17:42 UTC ( #1016122=note: print w/replies, xml ) Need Help??

in reply to Re: Selective printing of the Duplicates
in thread Selective printing of the Duplicates

I just put, the logic.. My code is a pretty big one, extracting from a DB and then trying to put it on a file. But, I'm stuck in this part of the logic wherein if I try to filter the duplicates always the 1st is picked the remaining is removed.. I wanted to know how can I ignore the 1st duplicate and pick the 2nd one..
• Comment on Re^2: Selective printing of the Duplicates

Replies are listed 'Best First'.
Re^3: Selective printing of the Duplicates
by choroba (Bishop) on Jan 30, 2013 at 17:45 UTC
If they are duplicates, how do you tell the 1st from the 2nd one?

Anyway, if you use only a part of a line to find the duplicates, it makes sense. You can either reverse the input file and use the old algorithm, or you have to remember the last line seen for each key in a hash. The next problem of the latter is to get the original order of the lines.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Selective printing of the Duplicates
by baxy77bax (Deacon) on Jan 30, 2013 at 18:27 UTC
Now is it the last one or the second one. Are these duplicates or multiplicities??

So, given that these are repeated several times and you need the second one I would use :

```my %seen = ();
my \$tmp

...
\$seen{\$_}++;
if (\$_ ne \$tmp && \$seen{\$tmp} == 1){
print \$_;
\$tmp =\$_;
}elsif (\$_ eq \$tmp){
print \$_ if \$seen{\$_} == 2;
}

however if it is the last one then:
```
my %seen = ();
my \$tmp;
my \$id;
...
if (\$_ ne \$tmp){
\$seen{\$tmp}=\$id++;
}
and then in the second loop:

print \$_ foreach (sort{\$seen{\$a}<=>\$seen{\$b}}keys %seen);

baxy

Create A New User
Node Status?
node history
Node Type: note [id://1016122]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-03-24 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (297 votes). Check out past polls.

Notices?