Here's another way to split one large HTML page down into smaller 56-line pages:

#!/usr/bin/perl -w use strict; use constant CHAPTER_LENGTH => 56; open INFILE, "< $ARGV[0]" or die "Could not open file $ARGV[0]: $!"; my $page = 1; until(eof INFILE) { open OUTFILE, "> chapterx_page_$page.html" or die "Could not open file chapter_$page: $!"; while(<INFILE>) { print OUTFILE; last unless $. % CHAPTER_LENGTH; #will return true when the line #no. is equally divisible by 56 } ++$page; }

This will iterate over each line in <INFILE>, and on every 56th line, it will start a new chapter. The last chapter will be 56 lines or less.

If your file size ever gets large I would suggest using this method. Reading a large file into an array, then slicing the aray after, will be more expensive than iterating over the source file one line at a time.


In reply to (dkubb) Re: (2) How do I split a file by lines into smaller files by dkubb
in thread How do I split a file by lines into smaller files by PatrioticDreamer

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, 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:
    & &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.