Hi, I've seen a few similar problems to this posted here recently but none of them cover what I need to do and my Perl isn't strong enough yet for me to figure out how to do this.
I'm parsing a number of text files (can range in size from 1K upto 100M) where the record size of each line is variable. I need to find (or seek to ?) the last occurence of a pattern then apply some further parsing logic from that point on until the end of file.
What I do now (which doesn't cope with the problem) is
$foundit = 0;
while (<$FH>) {
$foundit = 1 if /^pattern/;
}
if ($foundit) {
do some processing
}
This was working ok until I started to get multiple occurences of the pattern ,the files I'm parsing are now appended to rather than overwritten :(
I had an idea of making two passes on each file to be parsed. The first pass to ascertain how many occurences of pattern were in the file (set a count). The second to start parsing when the count was hit. e.g.
open FH,"file" or die "Can't open file : $!\n";
$count1 = 0;
while (<$FH>) {
$count++ if /^pattern/;
}
close FH;
open FH,"file" or die "Can't open file : $!\n";
$count2 = 0;
while (<$FH>) {
if (/^pattern/ and $count2 < $count1) {
$count2++;
} else {
do some processing
}
}
This is a possibility but it's very slow, which is why I wonder whether anybody has some other ideas about how to do this more efficiently ?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.