in reply to Delete last line of file with regex
Another variation on the "don't add the line to your string in the first place" concept, rather than using eof, would be to read the lines into an array instead of a scalar, remove the last entry from the array if it matches the regex, and then join the (remaining) lines into a single string:
my @lines = <DATA>; if ($lines[-1] =~ /^##/) { pop @lines; } my $slurp = join '', @lines;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Delete last line of file with regex
by afoken (Chancellor) on Jul 23, 2020 at 08:18 UTC | |
by Anonymous Monk on Jul 23, 2020 at 13:50 UTC | |
by Anonymous Monk on Aug 06, 2020 at 16:40 UTC |
In Section
Seekers of Perl Wisdom