First of all, you're processing a text file, so leave the MS Word stuff out of it. That's unnecessary and complicating things, as far as I can tell. I can see two ways to do this:

Process the file line-by-line, watching for lines starting with Function:. When you hit one, print it, then read in the next line (Input:) and save it, then read in the next line (Output:), stick a newline after the first colon, print it, then do the same with the Input: line you saved and print it, then print a new line. Start watching for a Function: line again.

The other way: read the whole thing into a single scalar variable, then write a regex to match the chunks of text that you're looking for, and loop through those chunks, printing out what you find in the format you want. Something like this (assumes a couple typos in your example):

while( $str =~ /(Function: .+?)\nInput: (.+?)\nOutput: (.+?)\n/gs ){ print <<END; $1 Output: $2 Input: $3 END }

In many languages you'd have no choice but to use the first method, which requires more programming logic. Perl's regexes are so powerful that you can replace a lot of logic with one big pattern match.

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re: Understanding Regex by aaron_baugher
in thread Understanding Regex by jack_64

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.