Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: how to find the unique lines in a file?

by Anonymous Monk
on Apr 22, 2009 at 13:15 UTC ( [id://759268]=note: print w/replies, xml ) Need Help??


in reply to how to find the unique lines in a file?

This solution guarantees that each 'sublist' that does appear will appear only once, and that you will get the longest lines. You may find, however, that some 'sublist's will not show up at all.
use warnings; use strict; my %data; my %uniq; my @final; while (<DATA>) { $data{$_}=()=$_=~m/\s+\b/g; } OUTER: foreach my $keys (sort {$data{$b} <=> $data{$a}} keys %data) { my @list=split(/\s+/,$keys); foreach (@list) { if (exists $uniq{$_}) { next OUTER; } } $uniq{$_}++ foreach (@list); push @final, $keys; } print @final; __DATA__ mylist_12 sublist153 sublist_34 sublist_123 sublist_345 sublist_245 mylist_1 sublist_153 sublist_87 sublist_876 sublist_78 mylist_6 sublist_8 mylist_2 sublist_12 sublist_34 sublist_09 mylist_3 sublist_87 sublist_09 mylist_7 sublist_8 sublist_9 mylist_9 sublist_56

Replies are listed 'Best First'.
Re^2: how to find the unique lines in a file?
by patric (Acolyte) on Apr 22, 2009 at 13:34 UTC
    brilliant..Awesome...thank you :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found