Hello Perlmonks,

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


In reply to Problems sorting by dr_jgbn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.