Is there a way to find out the number of lines in a file other than:
@lines = <FILE>;
$count = scalar(@lines);
The file can be rather large and I'd like to not waste the system's I/O time and memory with such a thing. As the second part of this question, is there some way to seek to a line, not number of bytes? I'd like to read in the last five or so lines in from a file that has several thousand without going through the entire beginning.
If there was any doubt, a line here is defined as the characters between newline characters, a length which is variable. I imagine there is no other way to do this because the file would have to be searched for a delimiter(in this case, a newline). Would a possible solution be set a buffer of, say, five thousand bytes to seek from the end of the file, assuming that such a number is longer than the last x lines? I guess I'm just looking for someone to agree with me or tell me I'm stupid, possibly offering a better soln. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
@lines = <FILE>;
$count = scalar(@lines);
The file can be rather large and I'd like to not waste the system's I/O time and memory with such a thing. As the second part of this question, is there some way to seek to a line, not number of bytes? I'd like to read in the last five or so lines in from a file that has several thousand without going through the entire beginning.
If there was any doubt, a line here is defined as the characters between newline characters, a length which is variable. I imagine there is no other way to do this because the file would have to be searched for a delimiter(in this case, a newline). Would a possible solution be set a buffer of, say, five thousand bytes to seek from the end of the file, assuming that such a number is longer than the last x lines? I guess I'm just looking for someone to agree with me or tell me I'm stupid, possibly offering a better soln. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light