Hi, I have a text file that I'm parsing. The file contents are in an @array, and I use a loop "for (@array) {" to get each line, using $_ as that line's contents.

The start of a line may contain a keyword and I take different steps based on those keywords. However, sometimes the text extends over multiple lines, and in those cases I think it would be useful to get the next line without leaving the outermost for loop. Maybe an example of the data would help:
keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1
In the above example, I'd like to parse keyword2 as if it were all on a single line. I do NOT want to do a "next" on the topmost for loop since I would lose the fact I'm parsing a keyword2 section.

"shift @array" gets me the next line in @array, BUT it throws away the first line in @array, which I don't want to do.

Suggestions are appreciated! Also, I can't think of the correct / succinct terminology other than "manually increment @array inside a for loop" so apologies if there's an answer already in here somewhere ;-)

In reply to Manually incrementing @ array during for by cniggeler

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.