use strict; use warnings; my @lines; { local *FILE; open(FILE, "< filename.in") or die("Couln't open input file: $!\n"); chomp(@lines = ); } my @sorted_lines = map { $_->[0]} sort { $a->[1] <=> $b->[1] } map { /^(\d+)/ && [ $_, 0+$1 ] } # OR: map { no warnings; [ $_, 0+$1 ] } @lines; { local *FILE; open(FILE, "> filename.out") or die("Couln't open output file: $!\n"); local $, = "\n"; print FILE (@sorted_lines); }