Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Handling A File In Human-Compatible Chunks by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others scrutinizing the Monastery: (1)
    As of 2024-07-21 23:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.