G'day cniggeler,

From your description, you're reading lines from a file and adding them to an array, then reading all the same lines from the array and processing them. You're doing the same work twice and you've provided no explanation why you need to do this. Is there a reason you're not just processing the lines as you read them from the file?

If your keyword lines all start the same -- e.g. "ID1", "ID2", etc. -- you can do something like this:

#!/usr/bin/env perl use strict; use warnings; { local $/ = 'keyword'; while (<DATA>) { next if $. == 1; chomp; y/\n//d; print "$/$_\n"; } } __DATA__ keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1

You may need to refer to local and, for $. and $/, "perlvar: Variables related to filehandles".

If the only way to differentiate keyword lines from continuation lines is by whitespace, you can do something like this:

#!/usr/bin/env perl use strict; use warnings; my $multiline = ''; while (<DATA>) { chomp; if (0 == index $_, ' ') { $multiline .= $_; } else { print "$multiline\n" if length $multiline; $multiline = $_; } } print "$multiline\n"; __DATA__ keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1

Both of those scripts produce identical output:

keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1

If there's something else going on here, you'll need to tell us. For instance, keyword may have some associated pattern, in which case a regex solution might be more appropriate.

Please include some code with any follow-up questions; along with output, even if that's only error messages.

— Ken


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