Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 (Chaplain) 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);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016122]
[marto]: yes, not reading the errors displayed has been a cause of this type of report in the past
[nysus]: Ambrus, ah. I think that was the problem. I'll try. Thanks!
[davies]: Ambrus: you've missed it by a couple of weeks, but consider next year's London Perl Workshop.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-12-15 11:37 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.