I have this little perl script that is supposed to sort a file:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
tie my @lines, 'Tie::File', 'fileToBeSorted.txt' or die $!;
printf "line count before: %d\n", scalar @lines;
@lines= sort @lines;
printf "line count after: %d\n", scalar @lines;
untie @lines;
When run with this input (fileToBeSorted.txt
)
one;4;1
two;3;2
three;2;3
four;1;4
the script outputs
line count before: 4
line count after: 5
and indeed, the sorted file contains an empty fifth line. Why is that and how can I prevent that?
sort grep { $_ } @lines
– Newberry