use strict; use warnings; my $fin = "SELECTDAT2"; my $fout = "myfile"; open my $ih, '<', $fin or die "cannot open $fin for reading, $!"; open my $oh, '>', $fout or die "cannot open $fout for writing, $!"; sub events { my @tokens = split; my @list = @tokens[0,3,4,7,8]; push @list; } while (<$ih>) { chomp; my @tokens = split; my @records = @tokens [3,4,5,6,10,11]; push @records; my $y = $tokens[0]; my $m = $tokens[1]; my $d = $tokens[2]; my $h = $tokens[3]; events; last; } print $oh events, "\n", @$_ for sort {$a->[3] <=> $b->[3]} @records; close ($oh); close ($ih);