giridharreddy9 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Please help...
by wind (Priest) on Mar 10, 2011 at 21:26 UTC

    What have you tried thus far? This feels like homework.

    Nevertheless, here's a recent thread where a monk was trying to accomplish a similar thing: Hash of Arrays

Re: Please help...
by NetWallah (Canon) on Mar 10, 2011 at 22:25 UTC
    One liner: (Change to single-quote for Linux):
    perl -ane "push @{$x{shift @F}},@F}{print qq|$_: @{$x{$_}}\n| for sort + keys %x" INPUTFILE.txt

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: Please help...
by choroba (Cardinal) on Mar 10, 2011 at 22:20 UTC
Re: Please help...
by mifflin (Curate) on Mar 10, 2011 at 21:38 UTC
    [tauser02@gfmwsp02lds bin]$ cat x use strict; use warnings; my %data; while (<DATA>) { my ($cluster, $unit) = split /\s+/; push(@{$data{$cluster}}, $unit); } for my $cluster (sort keys %data) { print $cluster, ' ', join(' ', @{$data{$cluster}}), "\n"; } __DATA__ Cluster1 unit1 Cluster1 unit2 Cluster1 unit3 Cluster1 unit4 Cluster1 unit5 Cluster2 unit6 Cluster2 unit7 Cluster2 unit8 Cluster2 unit9 Cluster2 unit10 [tauser02@gfmwsp02lds bin]$ perl x Cluster1 unit1 unit2 unit3 unit4 unit5 Cluster2 unit6 unit7 unit8 unit9 unit10
Re: Please help...
by locked_user sundialsvc4 (Abbot) on Mar 10, 2011 at 22:11 UTC

    As you can see from mifflin’s post, Perl actually makes this sort of thing quite easy.

    You clearly want the computer to build a “list of” the items (second column of input data) that appear under “keys” (first column).   Perl provides array (and list) data structures that allow you to easily manage “a list of zero-or-more things” and a hash data structure that lets you file things under arbitrary “keys.”

    The magic that really brings all of this together is the idea of references.   So, a hash can contain (for each key that it stores...) “a reference to” an array which contains all of the items belonging to that key.

    One of the most important features of a language like Perl is that it gives you a well-chosen set of memory data structures, plus a “garbage-collector based” memory management system that lets you string together these complicated data structures (almost...) without worrying about it.