I demo a common parsing pattern below. You figure out what is special about the start of a "new record". If you see that "special thing" and you are already working on a record, then you process the previous record and start a new one. Otherwise you are continuing the current record. Note that since the start of a new record triggers the output of the previous record, there is a need to output the final record once the data ends.

use strict; use warnings; $|=1; my $data_lines = ('keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1 '); my @lines = split (/\n/,$data_lines); print "To show array of text lines as per spec:\n"; foreach (@lines) { print " $_\n"; } print "\n"; print "Showing data array's per combined input lines:\n\n"; my @array = (); foreach my $line (@lines) { if ($line =~ /^\S/ and @array>0) # Finish previous record { process_array (@array); @array = (); #start new record push (@array,$_) foreach (split ' ',$line); } else # new or continuing record { push (@array, $_) foreach (split ' ',$line); } } process_array (@array); # the last record sub process_array { my @array = @_; print "process array in some sub = @array\n"; } __END__ To show array of text lines as per spec: keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1 Showing data array's per combined input lines: process array in some sub = keyword1 data1 data2 data3 process array in some sub = keyword2 data1 data2 data3 data4 data5 dat +a6 process array in some sub = keyword1 data1 data2 data3 data4 process array in some sub = keyword3 data1

In reply to Re: Manually incrementing @ array during for by Marshall
in thread 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.