#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data; while(){ chomp; my ($id, $sum, $haploStr) = split '\t'; $data{ $haploStr }{ count }++; push @{ $data{$haploStr}{id} }, $id; } for my $haploStr ( sort by_count keys %data) { print "$haploStr\tCount: $data{$haploStr}{count}\n"; print "\t$_\n" for @{ $data{$haploStr}{id} }; } sub by_count { $data{$b}{count} <=> $data{$a}{count} } __DATA__ 2_SecVal 4 1 1 0 0 0 1 0 0 1 0 1_SecVal 4 1 0 1 0 1 0 0 0 1 0 2_FirVal 4 1 0 1 0 1 1 0 0 0 0 3_FirVal 4 0 0 1 0 1 1 0 0 1 0 3_SecVal 4 0 0 1 0 1 1 0 0 1 0 2_FirVal 5 1 0 1 0 0 0 1 1 0 1 4_SecVal 5 1 0 1 0 1 1 0 0 1 0 5_SecVal 5 1 0 1 0 1 1 0 0 1 0 7_FirVal 5 1 0 1 0 1 1 0 0 1 0 4_FirVal 5 1 0 1 0 1 1 0 0 1 0 5_FirVal 5 1 1 0 0 0 0 1 1 0 1 6_FirVal 5 1 0 1 0 1 1 0 0 1 0 6_SecVal 5 1 0 1 0 0 0 1 1 0 1 7_SecVal 5 1 1 0 0 0 0 1 1 0 1