Your second example gets the first $n, not the last. Both are pretty wasteful of resources. Here's one way to do it,
I've localized $_ since while (<>) {...} does not, but that is usually not necessary.{ local $_; while (<FILEHANDLE>) { push @lines, $_ if /criteria/; shift @lines if @lines > $n; } }
After Compline,
Zaxo
In reply to Re: last $n lines that match a criteria
by Zaxo
in thread last $n lines that match a criteria
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |