Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Think about Loose Coupling
 
PerlMonks  

Please help...

by giridharreddy9 (Initiate)
on Mar 10, 2011 at 21:23 UTC ( #892536=perlquestion: print w/ replies, xml ) Need Help??
giridharreddy9 has asked for the wisdom of the Perl Monks concerning the following question:

I have a file in this format:


Cluster1 unit1
Cluster1 unit2
Cluster1 unit3
Cluster1 unit4
Cluster1 unit5
Cluster2 unit6
Cluster2 unit7
Cluster2 unit8
Cluster2 unit9
Cluster2 unit10


And I need to generate the output as:

Cluster1 unit1 unit2 unit3 unit4 unit5
Cluster2 unit6 unit7 unit8 unit9 unit10
I am new to perl and am not able to figure out how to do this...plz advice.

Comment on Please help...
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 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 sundialsvc4 (Monsignor) 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.

Re: Please help...
by choroba (Abbot) on Mar 10, 2011 at 22:20 UTC
Re: Please help...
by NetWallah (Monsignor) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://892536]
Approved by sundialsvc4
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-04-21 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls