G'day amitgsir,

There are a number of issues with your post:

The following code produces what I'm guessing is, at least, close to what you want. It produces the output in the same groupings you showshowed (see Update below), and it includes both the CLUSTER and DELIMIT tokens. Given the points I've already made regarding those tokens, this may not be exactly what you want: as stated, this is a guess on my part; modify to suit your needs.

#!/usr/bin/env perl use strict; use warnings; use constant { COL0 => 0, COL2 => 1, ALL => 2 }; my ($col0, $col2) = ('', ''); print map { if ($col0 ne $_->[COL0]) { ($col0, $col2) = @{$_}[COL0,COL2]; ("CLUSTER\n", $_->[ALL]); } elsif (substr($col2, 0, 3) ne substr($_->[COL2], 0, 3)) { $col2 = $_->[COL2]; ("DELIMIT\n", $_->[ALL]); } else { $_->[ALL]; } } sort { $a->[COL0] cmp $b->[COL0] || $b->[COL2] cmp $a->[COL2] } map { [ (split)[0,2], $_ ] } <DATA>; __DATA__ chrM:307 0 AGCGGGGA 129 chrM:307 0 AGCGGGGA 130 chrM:307 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 130 chrM:308 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 130 chrM:309 0 AGCGGGGA 129 chrM:307 0 TCAAAATG 130 chrM:308 0 TCAAAATG 130 chrM:309 0 TCAAAATG 130 chrM:307 0 TCACGGTG 130 chrM:308 0 TCACGGTG 130 chrM:309 0 TCACGGTG 130 chrM:307 0 TCAGCCTG 129 chrM:308 0 TCAGCCTG 129 chrM:309 0 TCAGCCTG 129 chrM:307 0 TCAGGGAG 130 chrM:308 0 TCAGGGAG 130 chrM:309 0 TCAGGGAG 130 chrM:307 1 TCAGGGTG 106 chrM:307 2 TCAGGGTG 130 chrM:307 2 TCAGGGTG 129 chrM:308 1 TCAGGGTG 106 chrM:308 2 TCAGGGTG 130 chrM:308 2 TCAGGGTG 129 chrM:309 1 TCAGGGTG 106 chrM:309 2 TCAGGGTG 130 chrM:309 2 TCAGGGTG 129

Output:

CLUSTER chrM:307 1 TCAGGGTG 106 chrM:307 2 TCAGGGTG 130 chrM:307 2 TCAGGGTG 129 chrM:307 0 TCAGGGAG 130 chrM:307 0 TCAGCCTG 129 chrM:307 0 TCACGGTG 130 chrM:307 0 TCAAAATG 130 DELIMIT chrM:307 0 AGCGGGGA 129 chrM:307 0 AGCGGGGA 130 chrM:307 0 AGCGGGGA 129 CLUSTER chrM:308 1 TCAGGGTG 106 chrM:308 2 TCAGGGTG 130 chrM:308 2 TCAGGGTG 129 chrM:308 0 TCAGGGAG 130 chrM:308 0 TCAGCCTG 129 chrM:308 0 TCACGGTG 130 chrM:308 0 TCAAAATG 130 DELIMIT chrM:308 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 130 chrM:308 0 AGCGGGGA 129 CLUSTER chrM:309 1 TCAGGGTG 106 chrM:309 2 TCAGGGTG 130 chrM:309 2 TCAGGGTG 129 chrM:309 0 TCAGGGAG 130 chrM:309 0 TCAGCCTG 129 chrM:309 0 TCACGGTG 130 chrM:309 0 TCAAAATG 130 DELIMIT chrM:309 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 130 chrM:309 0 AGCGGGGA 129

By the way, the construct I've used in the form of

... map { ... } sort { ... } map { ... } ...

is known as a Schwartzian Transform. It is particularly useful for sorting based on parts of a string while retaining the entire string, in an unmodified form, for eventual output.

Update: The original, expected output has changed. There is no notification of what changed or even that a change has been made. Here's the original (in the spoiler) that my response is based on:

— Ken


In reply to Re: Find duplicate based on specific fields while allowing 2 mismatch by kcott
in thread Find duplicate based on specific fields while allowing 2 mismatch by amitgsir

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.