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
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.