dr_jgbn has asked for the wisdom of the Perl Monks concerning the following question:
I am having a problem figuring out how to use the sort function within the context of one of my scripts.
Here is the code to start off with:
use strict; use vars qw($outfile); open(DATA, $ARGV[0]) or die "Couldn't open $ARGV[0]: $!"; $outfile = "test.txt"; open (OUT, ">$outfile") || die "Can't open $outfile for creation: $!\n +"; my (%hash,@order); while(<DATA>) { chomp; my @bits = split /\t/; my $first = "$bits[0]"; push @order, $first unless exists $hash{$first}; $hash{$first} .= ' ' . "$bits[1] $bits[2]\t"; } print OUT "$_\t$hash{$_}" for @order;
So for example, the script takes a file that looks like this:
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
Output:
text1 text-a text-w text-d text-n
text2 text-b text-y
text3 text-x text-t text-f text-z text-e text-w
The problem that I am having is to just sort the concatenated data in each row.
i.e. I would like the data to look like this,
text1 text-a text-d text-n text-w
text2 text-b text-y
text3 text-e text-f text-t text-w text-x text-z
I should mention that $bits[1] is always a word and $bits[2] is always a word followed by a space then a number (e.g. dehydration 1)
Any help would greatly be appreciated,
Thank-you in advance,
Dr.J
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems sorting
by Limbic~Region (Chancellor) on Jun 01, 2003 at 15:12 UTC | |
|
Re: Problems sorting
by edoc (Chaplain) on Jun 01, 2003 at 15:33 UTC | |
by Limbic~Region (Chancellor) on Jun 01, 2003 at 15:38 UTC | |
|
Re: Problems sorting
by Skeeve (Parson) on Jun 02, 2003 at 07:14 UTC |