in reply to Using file handles

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).

Replies are listed 'Best First'.
Re^2: Using file handles
by ikegami (Patriarch) on Nov 23, 2004 at 23:29 UTC

    That should be
    sort {$$b[1] <=> $$a[1]}
    not
    sort {@$b[1] <=> @$a[1]}