Use pseudocode to get a better general grasp of the problem.

First, restate your project description more accurately (in English). Only one of the three things you actually do when the current line is a chapter end. Here's the best I could make of it:

  1. When the current line is a chapter end, insert a chapterization command.
  2. In the first two pages of a chapter, change the paper output tray.
  3. On the third page following the end of a chapter, insert a command to send the newly started chapter to the binder.

Then translate that to pseudocode. I don't really recommend using obscenely long variable and subroutine names, but the point is that you should factor out the little pieces so that you can look at the structure of the loops and conditionals all at once.

my $pages_this_chapter = 0; while (<IN>) { if (current_line_is_the_kdkhost_line()) { next } # Don't forget the special case # if (current_line_is_a_chapter_end() || $. == 1) { print OUT $chapterization_command; $pages_this_chapter = 0; next; # ??? } if (current_line_starts_a_new_page()) { $pages_this_chapter++; if ($pages_this_chapter == 3) { print OUT $send_to_binder } } if ($pages_this_chapter <= 2) # 3? { change_the_paper_output_tray() } print OUT; }

That's not exactly what you want, but you get the idea. Now fill in the blanks. Remember that $_ and $. are visible everywhere. (You may prefer to change your subroutines to accept the current line and/or line number as arguments instead of peeking at $_ and $. .)

sub current_line_is_the_kdkhost_line { m/^%KDKHost:/; } sub current_line_is_a_chapter_end { m!<</OutputType \(\)>>setpagedevice!; } sub current_line_starts_a_new_page { /%%BeginPageSetup/; } sub change_the_paper_output_tray { m!<</OutputType\(Stacker\)>>setpagedevice! and s/Stacker/top/; }

-- 
LP^>


In reply to Re: Can I do an "inner read"? by TilRMan
in thread Can I do an "inner read"? by digger

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.