Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Loading a part of the file to array using Tie::Fileby kcott (Archbishop) |
on Nov 24, 2017 at 00:02 UTC ( [id://1204180]=note: print w/replies, xml ) | Need Help?? |
G'day ansh007, Welcome to the Monastery. Take a look at "perlop: Range Operators" and the eof function. Your "from line number 100 till end" can be written in Perl as "100 .. eof". Given this file:
And an alias I use frequently:
You can read from line 3 to the end like this:
That also works with a literal line number instead of eof. For example, to print lines at the start, or in the middle:
For those last two, once you've read all the wanted lines, you can exit the while loop early with the last function. See also: open for a better way to open files; the pragma index for links to strict, warnings and autodie (you should always use the first two; I highly recommend the third for simple I/O error checking); and, if you're unfamiliar with the "-M" and "-E" switches I've used, perlrun. — Ken
In Section
Seekers of Perl Wisdom
|
|