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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.