eileen1017
MIS
I have a document with its last line empty. How can I use perl to ask it to read through the whole document and remove the last empty line? I don't whole how many exact lines are in the document.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
open(IN,"<some_document.txt");
@lines = <IN>; # dump all doc lines in an array
close IN;
pop @lines; # takes the last line off and throws it away
# assemble all lines into a single string, for convenience
$doc = join("",@lines);
# print doc contents, minus last line, back
# to original file.
open(OUT,">some_document.txt");
print OUT $doc;
close OUT;
open(READFILE,"<somefile.txt");
while(<READFILE>){
chomp; #cut off \n
next unless length;
#then do something else if it has length
push @lines;
}
close READLINE;
# dump the file back out
$lines = join(/\n/,@lines);
open(OUT,">somefile.txt");
print OUT $lines;
close OUT;