Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Selective printing of the Duplicates

by Anonymous Monk
on Jan 30, 2013 at 17:26 UTC ( #1016115=note: print w/ replies, xml ) Need Help??


in reply to Selective printing of the Duplicates

But, it doesnt give me the desired result...

Of course it doesn't, you never read a file


Comment on Re: Selective printing of the Duplicates
Re^2: Selective printing of the Duplicates
by Thomas Kennll (Acolyte) on Jan 30, 2013 at 17:42 UTC
    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..
      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.

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016115]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-10-23 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls