G'day catemp,

Welcome to the Monastery.

After looking through the thread, I couldn't see any specifics about how you wanted the output ordered. Here's three solutions with different output orders; they become progressively more complicated and, accordingly, will take longer to run.

I started with the data you posted below. I then added a few more lines for test purposes:

6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8

appears three times: two adjacent; one separated.

6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788

is a new substring of others starting with 6Y0248 but is separated from them; it's the new first record.

Here's the code containing all three solutions:

#!/usr/bin/env perl use strict; use warnings; my @in_data = qw{ 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 2N0472|6N8595|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 3419308|3514531|3525716|3557019|3586192|3635776|3783741 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721 3T3628|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152 }; print "Input data:\n", '-' x 40, "\n"; print "$_\n" for @in_data; { my @out_data; print '-' x 40, "\nOutput data (unordered):\n", '-' x 40, "\n"; my $long_string; for my $string (reverse sort @in_data) { if (defined $long_string) { next if index($long_string, $string) == 0; } $long_string = $string; print "$string\n"; } } { my @out_data; print '-' x 40, "\nOutput data (ordered by first field):\n", '-' x + 40, "\n"; my $long_string; for my $string (reverse sort @in_data) { if (defined $long_string) { next if index($long_string, $string) == 0; } $long_string = $string; push @out_data, $string; } print "$_\n" for sort @out_data; } { my @out_data; print '-' x 40, "\nOutput data (original order):\n", '-' x 40, "\n +"; my $long_string; my $pos = 0; for my $pos_string (sort { $b->[1] cmp $a->[1] } map [ $pos++, $_ +], @in_data) { if (defined $long_string) { next if index($long_string, $pos_string->[1]) == 0; } $long_string = $pos_string->[1]; push @out_data, $pos_string; } print "$_->[1]\n" for sort { $a->[0] <=> $b->[0] } @out_data; } print '-' x 40, "\n";

Note the use of anonymous blocks to limit the scope of identically named variables (e.g. @out_data and $long_string).

Here's the output:

Input data: ---------------------------------------- 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 2N0472|6N8595|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 3419308|3514531|3525716|3557019|3586192|3635776|3783741 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721 3T3628|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152 ---------------------------------------- Output data (unordered): ---------------------------------------- R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 3T3628|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721 3419308|3514531|3525716|3557019|3586192|3635776|3783741 2N0472|6N8595|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 ---------------------------------------- Output data (ordered by first field): ---------------------------------------- 2N0472|6N8595|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 3419308|3514531|3525716|3557019|3586192|3635776|3783741 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721 3T3628|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 ---------------------------------------- Output data (original order): ---------------------------------------- 2N0472|6N8595|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 3419308|3514531|3525716|3557019|3586192|3635776|3783741 3T3625|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721 3T3628|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 4N4906|6N6481|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788 6N7936|6N5049|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8 R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|2757803 6Y0248|6T7765|9L1366|1189902|1413983|8B2026|1M3381|7K3377|3H5788|1F785 +4|8W1152|8R0721|9C5344|6W6672|9G7101|3023908|6Y1352|4P0489|1336934 ----------------------------------------

— Ken


In reply to Re: Best method to eliminate substrings from array by kcott
in thread Best method to eliminate substrings from array by catemp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.