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.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
If you want to preserve the other fields and just output the data in order:
This pushes an array of the split fields instead of just the age, then sorts on the second item of each nested array (age).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
In reply to Re: Using file handles
by TedPride
in thread Using file handles
by gitarwmn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |