in reply to Find duplicate based on specific fields while allowing 2 mismatch

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

Replies are listed 'Best First'.
Re^2: Find duplicate based on specific fields while allowing 2 mismatch
by amitgsir (Novice) on Aug 29, 2017 at 00:51 UTC

    Thanks for your time and sorry I missed to update without keeping spoiler tag.

    I tried to explain the question again with more detail.

    It is interesting to learn the map contruct, I was trying to do with just using Arrays which was I guess not efficient. I am learning more about 'hash' and 'map' using perlmonks suggestion. Thanks!