It makes no sense to read a large file a byte at a time, on a system where you use perl. 3GB is a bit much to hold in memory at once, at least it is for me, but a buffer of a few k to a few tens of k should work really well. Also, there's no need for the two handles... use the same buffer for both.
My idea is to fill the buffer, look for the start character, and if you find it too close to the end of the block, read some more and append it to the buffer. read() even supports this operation out of the box.
Sample code (not tested well):
my $windowsize = 500;
use constant BLOCKLENGTH => 4096;
my $buffer = "";
my $offset = 0;
while(my $r = read FH, $buffer, BLOCKLENGTH) {
my $i = -1;
until(($i = index($buffer, "x", ++$i)) < 0) {
printf "found 'x' at %d+%d\n", $offset, $i;
if($i+$windowsize > length $buffer) {
# get rid of what we no longer need, or we might end up wi
+th a buffer holding the whole huge file:
$offset += $i;
$buffer = substr $buffer, $i;
$i = 0;
# append a new buffer (assuming BLOCKLENGTH >= $windowsize
+):
$r = read FH, $buffer, BLOCKLENGTH, length $buffer;
last if $windowsize > length $buffer; #not long enough
}
# do something here...
printf "offset for 'x' is %d, found '%s' at %d\n",
$offset + $i, substr($buffer, $i + $windowsize - 1, 1),
$offset+$i+$windowsize-1;
}
} continue {
$offset += length $buffer;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.