On Thu, May 15, 2008 at 12:13 PM, ANJAN PURKAYASTHA <
[EMAIL PROTECTED]> wrote:

> Hi,
> here is a problem I'm working on. It's not PERL-specific, rather it is a
> problem in sorting followed by grouping.
> Suppose I have a set of lines that have tab-delimited text, thus:
> 1  w   3   wer
> 2  a   4    rte
> 4  w   2    weg
> 6  d  4    fhg
> 5  d   7   dfl
> 6  w   4   ald
> 8  a   3   dsl
>
> I would like to first sort the lines based on the 2nd token (w,a, w, d,
> etc)
> and then group the lines based on the 2nd token.
>
> At the end of this sorting/grouping I should have the lines grouped thus:
> 2  a   4    rte
> 8   a   3   dsl
>
> 6   d  4    fhg
> 5   d   7   dfl
>
> 1  w   3   wer
> 4  w   2    weg
> 6  w   4   ald
>
>
> I can figure out the sorting. Are they any command/modules to do the
> grouping based on identical tokens?
>
> appreciate your input.
>
>
> tia,
> anjan
>
>
>
> --
> ANJAN PURKAYASTHA, PhD.
> Senior Computational Biologist
> ==========================
>
> 1101 King Street, Suite 310,
> Alexandria, VA 22314.
> 703.518.8040 (office)
> 703.740.6939 (mobile)
>
> email:
> [EMAIL PROTECTED];
> [EMAIL PROTECTED]
>
> http://www.vbi.vt.edu
>
> ==========================
>


hash of array will solve your problem

use strict;
use warnings;

my %HofA;

map { push @{ $HofA{ (split)[1] } },  $_  }  <DATA> ;

for my $key (sort keys %HofA) {
  print $_ for @{ $HofA{$key} };
  print "\n";
}


__DATA__
1  w   3   wer
2  a   4    rte
4  w   2    weg
6  d   4    fhg
5  d   7   dfl
6  w   4   ald
8  a   3   dsl

Reply via email to