Try to base your code on the attached. Couple of things:
  1. You have to consider that, your data may come in out of sequence, DATA SET 10 might come before DATA SET 9, so you have to determine the index, before you can add the string to the array.
  2. Also you may have some data missing, for example, you may not have DATA SET 7 between DATA SET 6 and 8, you need to take this into consideration. Again this makes it a must for you to determine the index on fly.
  3. Is it possible for you to have the same DATA SET number more than once? For example, you had ">DATA SET 10", and later have "DATA SET 10" again. If yes, you have to think of a way to handle it base on the requirement. In my code, I just concat the latest with all old ones.
  4. In my code, I do not chomp away the newlines. If you want to remove them, uncomment that chomp.
  5. In my code, I left the array element at index zero undef all the time. If you want, you can use it, just substract 1 from the number you read from the file.
  6. in your m//, that \ before > is not needed, although it does not cause problem.
use Data::Dumper; use strict; my @data; my $cur_index; open(DATA, "<", "data.txt"); while (<DATA>) { #chomp; if (m/^>DATA SET (\d+)/) { $cur_index = $1; } else { $data[$cur_index] .= $_; } } close(DATA); print Dumper(@data);
(UPDATE: When the question was first posted, the data part was not well formatted as it is now, and my first solution assumed that the real data came right after the "DATA SET n", without line broken. Obviously that solution was wrong.

Thanks to gjb, he sent me a message, and pointed out that my solution didn't make sense to him. Then I checked the original question, and realized the format is now different after adding tags.

I really appreciate, not just gjb's tech point, but more important the way he handled it, which clearly shows his pleasant personality.)


In reply to Re: writing to arrays by pg
in thread writing to arrays by Superman

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.