hello wise Monks,

I am trying to read lines from a file which looks like this:

Description:
yada
yada
yada
yada
Data:
yada
yada
yada
yada

I need to put the lines between "Description:" and "Data:" in one Array and the lines after "Data:" and before EOF into a Second Array.

I use regular expressions to do this, but this does not seem to be efficient enough since I have to read the file into a string and then split it up as in...

open(R, filename); while(<R>) { $temp .= $_; } close(R); if($temp =~ /Description:(.*)Data:(.*)/s) { $result1 = $1; push (@array1, $result1); $result2 = $2; push (@array2, $result2); }

Instead I want to parse the file line by line and try to put the lines from the file into the arrays without first reading the whole file into a scalar...

Like maybe using some flags (someone suggested), but how?

Please not that the file will be of the format as mentioned above, but new tags maybe added, then the number of arrays to put the lines... will increase....

Thank you all for time and help...

A learning Monk...


In reply to How to read lines from a file which is.... by ginju75

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.