in reply to clustering pairs

clustering is best left to a module, imho.

use Graph; use strict; use warnings; my %v; while (<DATA>) { for my $id ( /(\.C\S+)/g ) { push @{ $v{$id} }, $_; } } my $g = new Graph::Undirected; for my $id ( keys %v ) { my @v = @{ $v{$id} }; $g->add_vertex( $v[0] ) if @v == 1; while ( @v > 1 ) { $g->add_edge( $v[0], $v[1] ); shift @v; } } my @clusters = $g->connected_components; for my $i ( 0 .. $#clusters ) { print "\ncluster".($i+1)."\n"; print for @{$clusters[$i]}; } __DATA__ ID5141.C1665 ID5141.C2448 ID5141.C1253 ID5144.C2039 ID5141.C1596 ID5144.C1956 ID5141.C1906 ID5144.C2149 ID5141.C1221 ID5144.C1956 ID5141.C2149 ID5141.C2386 ID5141.C2039 ID5142.C1221 ID5141.C5887 ID5141.C7685 ID5141.C1005 ID5142.C2808 ID5141.C1046 ID5141.C1596 ID5141.C2386 ID5141.C4990 ID5141.C7685 ID5141.C4888
Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.