You were sorting a three element array containing a name, a city, and an age. The fact that you were trying to sort the array before completely reading in the file should have been a dead giveaway. You need to create an Array of Arrays (AoA), as described in perllol.
chomp removes the newline.
#!/usr/local/bin/perl use strict; use warnings; my $file = "input.log"; open(IN, $file) || die "Couldn't find '$file': $!"; my @age; while (my $line = <IN>) { chomp($line); my ($name, $age) = split(/\s*,\s*/, $line); push(@age, [ $name, $age ]); } close(IN); my @sorted_ages = sort { $b->[1] <=> $a->[1] } @age; foreach my $a (@sorted_ages){ print $a->[0], ' is ', $a->[1], ' years old.', $/; }
btw, it's better to store birthdate/birthyear than age and calculate the age when needed, so that you don't have to update your records every year.
In reply to Re: Using file handles
by ikegami
in thread Using file handles
by gitarwmn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |