Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Remove duplicate lines in a file

by RhetTbull (Curate)
on Nov 05, 2008 at 18:14 UTC ( #721755=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Remove duplicate lines in a file
in thread Remove duplicate lines in a file

I'm not sure I understand the question. I think you're asking how to print the last entry instead of the first. This code should do that:

use strict; use warnings; my $last_key = undef; my $last_line = <>; #get first line while(<>) { my $key = (split)[0]; if (defined $last_key && $key ne $last_key) { #new key, print the last line from the old key print $last_line; } $last_line = $_; $last_key = $key; } print $last_line; #very last entry won't get printed in the while loop


Comment on Re^3: Remove duplicate lines in a file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2015-07-07 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls