Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

splitting up a long text at regular intervals

by Anonymous Monk
on Oct 30, 2005 at 23:14 UTC ( #504072=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

salve monks,

I have the text of a play which I was to put into a presentation. So I need to split it up every 15 lines or so.

I had already stated to do this in vim by recording a macro where I would insert some special text say xx (which I can then parse upon) after I'd moved down around 15 lines.

Can this be done in perl (more automatically I guess)? Also does it complicate things to not want to introduce the parsing marker when the 15 line account occurs in the middle of a paragraph?

Sorry for not having a good grasp of the parsing lingo here, but hopefully you get what I want to do.
  • Comment on splitting up a long text at regular intervals

Replies are listed 'Best First'.
Re: splitting up a long text at regular intervals
by ioannis (Abbot) on Oct 31, 2005 at 00:40 UTC
    Since preserving paragraphs is more important than exact linecount; this is about what you want: keep reading paragraphs, until they exceed a predefined number of lines.

    use Fatal qw(open); use constant MAXLINES => 2; open my $i, 'data.txt'; (local $/, my ($sum, $buf)) = ( '', 0 , undef); while (<>) { $buf .= $_; $sum += tr /\n/\n/; next if $sum < MAXLINES ; print "$buf----------";; ($sum, $buf) = (0, undef ); }
Re: splitting up a long text at regular intervals
by GrandFather (Saint) on Oct 30, 2005 at 23:58 UTC

    Something like this?

    use warnings; use strict; my $linesSinceBreak = 0; while (<DATA>) { chomp; $linesSinceBreak = 0, print "\n<<BreakHere\n" if ++$linesSinceBreak +>= 15 && ! length $_; print "$_\n"; }

    Perl is Huffman encoded by design.
Re: splitting up a long text at regular intervals
by EvanCarroll (Chaplain) on Oct 30, 2005 at 23:25 UTC
    I'm not sure as to what exactly your asking, but allow me to point you into the direction of unless ($. % 15) {}

    Evan Carroll

      $. is error prone, as one can have multiple files open in a program, but there is only one $. I would rather call input_line_number() with specific file handler.

      use IO::Handle; open(MYSELF1, "<", ""); open(MYSELF2, "<", ""); while (<MYSELF1>) { print "after reading from myself1: $.\n"; if ($. % 2) { <MYSELF2>; print "after reading from myself2: $.\n"; } print "returned values from input_line_number: ("; print MYSELF1->input_line_number(), ", "; print MYSELF2->input_line_number(), ")\n"; } close(MYSELF1); close(MYSELF2);
Re: splitting up a long text at regular intervals
by Cody Pendant (Prior) on Oct 30, 2005 at 23:58 UTC
    I had a question like this, where I wanted to read from a file in chunks, not specified by number of lines but by number of bytes, but still, you might find it interesting.

    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: splitting up a long text at regular intervals
by stabu (Scribe) on Oct 31, 2005 at 10:03 UTC
    I was the OP, except I didn't log in.

    I want to say thanks for all the answers. I have plenty to work on. I'm beginning to think that perlmonks is really the tops, each time I get a fww quality answers even when I hurry my question. One for the rare sites where energy and quality coexist. Congrats to all.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://504072]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-11-26 19:02 GMT
Find Nodes?
    Voting Booth?