Assuming you just want the ages:
use strict; use warnings; my @ages; while (<DATA>) { chomp; push @ages, (split /[, ]+/)[1]; } print join "\n", sort {$b <=> $a} @ages; __DATA__ Charley, 34 Sam, 3 Lucy, 18
For each line, this trims the newline off the end, then splits on one or more commas or spaces and pushes the second item into @ages. @ages is then sorted numerically from largest to smallest and output with one age per line.

If you want to preserve the other fields and just output the data in order:

use strict; use warnings; my @ages; while (<DATA>) { chomp; push @ages, [split /[, ]+/]; } for (sort {@$b[1] <=> @$a[1]} @ages) { print join (', ', @$_) . "\n"; } __DATA__ Charley, 34 Sam, 3 Lucy, 18
This pushes an array of the split fields instead of just the age, then sorts on the second item of each nested array (age).

In reply to Re: Using file handles by TedPride
in thread Using file handles by gitarwmn

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.