in reply to Problems sorting
bah! L~R beat me to it again..
in the spirit of TIMTOWTDI...
..note I've swapped '\t' for ',' to make it easily copy/pasted..
#!/usr/bin/perl my %hash; while(<DATA>){ chomp; my @line = split /,/; my $first = shift @line; push( @{$hash{$first}}, @line ); } foreach( sort keys %hash ){ print "$_\t".join(' ', sort @{$hash{$_}})."\n"; } __DATA__ text1,text-a,text-w text2,text-b,text-y text3,text-x,text-t text1,text-d,text-n text3,text-f,text-z text3,text-e,text-w text10,word,other 1 text10,aword,nother 1 text11,cword,tother 1 text11,zword,bother 1 __END__ prints: text1 text-a text-d text-n text-w text10 aword nother 1 other 1 word text11 bother 1 cword tother 1 zword text2 text-b text-y text3 text-e text-f text-t text-w text-x text-z
Update: hmm.. this version doesn't produce the lines in the order it found them (it sorts but the first word in the line), but that's easily changed if required as per the other examples..
cheers,
J
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Problems sorting
by Limbic~Region (Chancellor) on Jun 01, 2003 at 15:38 UTC |