At the risk of posting code that you possibly can't use, how about a completely different approach. Instead of storing the names in one array and the items in other, how about creating a data structure that is a List of Hashes (LOH). Each hash has a 'name' key that points to a scalar, and an 'item' key that points to an array reference. Now you have a usuable data structure.

The idea is to store the names and the items into the hash, and when a new record is read, simply store the hash. The problem is that you have (well, i have to - this is an invitation for a more elegant solution should someone wish to show me a new trick ;)) discard the first line and store the last record outside of the while loop:

use strict; use Data::Dumper; my (@record, $discard, %temp); open(FH, 'data') or die 'file not found'; # discard the first line $discard = <FH>; while(<FH>) { chomp; s/\cM//; if (/^\d-RECORD$/) { push @record, {%temp}; undef %temp; } elsif (/ITEMNAME-(.*)/) { $temp{name} = $1; } elsif (/ITEM \d-(.*)/) { push @{$temp{item}}, $1; } } # the last record needs to be added outside while loop push @record, {%temp}; print Dumper \@record; # print items of last record print join(', ', @{$record[-1]{item}}), "\n";
If someone knows of a more elegant way, please share. I tried using File::ReadBackwards, but then extra work is needed to reverse the items ...

UPDATE:
Regarding your regexes - you might want to consider changing \d to \d+ so that you can catch numbers greater than 9, and you probably should use \s or \s+ instead of a literal space.

UPDATE: UPDATE:
Thanks belg4mit!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: How do I re-read a line? by jeffa
in thread How do I re-read a line? by rbc

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.