Firstly parse the temp file
open(my $fh, "tempfile") or die("ack - $!"); chomp( my $header = <$fh> ); my(undef, @headers) = split ' ', $header; my(%cols) = do { my $i = 0; map { $_ => $i++ } @headers; }; my %info; while(<$fh>) { chomp; next if m< ^ \s* \z >x; my($name, @rows) = split; push @{ $info{$name} }, @rows; } close $fh;
Then sort on the 'z' column
open(my $fh, ">", "output") or die("ack - $!"); print $fh $header; print $fh "$_ @{ $info{$_} }\n" for reverse sort { $info{$a}->[ $cols{z} ] <=> $info{$b}->[ $cols{z} ] } keys %info; close $fh;
Of course a much better idea would be to use one of the CSV type modules such as tilly's Text::xSV or Text::CSV_XS.
HTH

_________
broquaint

update: changed header regex => split, as I had a bad meme about multiple matches pushing onto the return list, unfortunately not e.g

my(@list) = "foo bar baz" =~ m< (?: (\w+) \s* )+ >x; print "@list"; __ouput__ baz # not - foo bar baz

update 2: posting on a Friday != a good idea

my(@list) = "foo bar baz" =~ m< (\w+) [ ]? >xg; print "@list"; __output__ foo bar baz
the previous example not working presumably because it was missing the g modifier, and the subsequent captures over-rode the previous one.

In reply to Re: File Sorting by broquaint
in thread File Sorting by rchou2

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.