You need to split the lines as you read them, then use a sort with a custom sort function.
open FH, "<", $filename; my @record = sort { $b->[2] cmp $a->[2] } map { [ chomp; split ] } <FH +>; close FH;

After that, you have a sorted list of records in @record.

Update: Oh yes.. a couple mistakes there. I wasn't going for a full Transform though. :) As I said, @record contains records; not the lines from the input. Of course, a Schwartzian Transform is the simpler solution when you don't need to process the thawed data inbetween.

Some explanation: map applies the following code block to every item from the list after it. Since we say <FH> there, the list is simply all lines of the file. The code inside the block removes the newline from the lines by way of chomp, then uses split to split the string into a list of strings. The default assumes you split on whitespace; if your field delimiters aren't blanks, you can supply different ones, see the doc. After that, the square brackets create an anonymous array and produce a reference to it. Since we did that for every line, the input to sort is going to be a list of references. Inside the sorting routine, $a and $b point to the elements to be compared; we dereference them by using the arrow operator, and pick the 3rd field from each. Then we use cmp to compare them, and because we want decending order we compare $b with $a rather than vice versa.

Makeshifts last the longest.


In reply to Re: File Sorting Rephrased by Aristotle
in thread File Sorting Rephrased 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.