|
|
Question : taking multiple lines of file at a time.............
|
|
I am trying to read a file, and actually instead of getting one line at a time, specify how many lines at a time:
example :
instead of this:
DATA = "<$file"; while () { } something like DATA ="<$file"; while () { } Is this possible?
|
Answer : taking multiple lines of file at a time.............
|
|
There are several different ways you could approach this. Perhaps the simplest would be to read the entire file into memory ("slurp mode") and use 'splice' to grab the first n lines of this array. Something like:
my @AllData = ;
while( @AllData ) { my @TenLines = splice( @AllData, 0, 10); . . . # do your thing with the @TenLines array here . . }
Another approach might be to use Tie::File this use the same array paradigm, but avoids reading the whole file into memory.
The problem with doing an explicit loop to read the n lines in is that you have to be prepared for end-of-file, which makes the business messier if it can occur in the middle of the n lines you were expecting to read.
If your data file has some sort of natural delimiters in it that could be used to bracket what needs to be read in each iteration through the loop, there are additional methods that might be used involving $INPUT_RECORD_SEPARATOR or range operators, e.g. /BEGIN/ .. /END/.
|
|
|
|
|