I want to process a text file in human-compatible chunks of a certain size or below, as in, not "sysread" for chunks of 2048 bytes, but something which won't have end up dividing words or sentences.
By which I mean I want to go paragraph by paragraph, until I get to a chunk of a certain size, so I got something like this:
open( X, 'x.txt' );
my ( $chunk, $line );
while (<X>) {
$line = $_;
if ( ( length($chunk) + length($line) ) > 2048 ) {
# if the next line would take us over the set size
doSomethingWithChunk($chunk);
$chunk = $line;
}
else {
$chunk .= $line;
# append line and keep going
}
}
doSomethingWithChunk($chunk);
# process whatever's left in $chunk at the end
which is fine, but what I'd really like to do is have some kind of sub which would return me those chunks until the file ran out.
Something like how Algorithm::Permute does this:
my $p = new Algorithm::Permute(['a'..'d']);
while (@res = $p->next) {
# do something with @res
}
Is there a module or a recognised way to do this? I'm blanking on the way to do it.
($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print
-
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.
|