This way avoids all the loops you have in your solution.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data; while(<DATA>){ 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
Output
1 0 1 0 1 1 0 0 1 0 Count: 5 4_SecVal 5_SecVal 7_FirVal 4_FirVal 6_FirVal 1 0 1 0 0 0 1 1 0 1 Count: 2 2_FirVal 6_SecVal 1 1 0 0 0 0 1 1 0 1 Count: 2 5_FirVal 7_SecVal 0 0 1 0 1 1 0 0 1 0 Count: 2 3_FirVal 3_SecVal 1 0 1 0 1 0 0 0 1 0 Count: 1 1_SecVal 1 0 1 0 1 1 0 0 0 0 Count: 1 2_FirVal 1 1 0 0 0 1 0 0 1 0 Count: 1 2_SecVal

In reply to Re: Extract the common values in hash of array with double keys by Cristoforo
in thread Extract the common values in hash of array with double keys by snape

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.