in reply to Using file handles

Hi,

I don't think I understand your code... for each line in the file, you are splitting on a comma, expecting a name, city, and age. But, in your sample data, you only have name followed by age. If your sample data is a sample of your log file, then let's try this code:

use strict; my $file = "input.log"; my @sorted_ages; open (IN, $file) or die "Couldn't open file '$file': $!"; while (my $line = <IN>) { chomp($line); my ($name, $age) = split(/,\s*/, $line); push @sorted_ages, $age; } close (IN); @sorted_ages = sort { $b <=> $a } @sorted_ages;

So, this goes: Split each line in the file by a comma* which will return the name, followed by the age. Then put that age in the array @sorted_ages. At the very end of the loop, we sort @sorted_ages.

* the \s* in the split makes sure to take up any spaces between the comma and age. We also chomp the line before splitting to remove the trailing new line.

If you follow so far, we can simplify this code a bit:

use strict; my $file = "input.log"; my @sorted_ages; open (IN, $file) or die "Couldn't open file '$file': $!"; @sorted_ages = sort { $b <=> $a } map { # Use a regex to find the age # ^ = start of string, # .*?, = skips to the first comma # (\d+) = capture a number into $1 /^.*?, (\d+)/; $1 } <IN>; close (IN);

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)