I'm not sure what to say to answer your actual questions, but I suspect that
$line =~ s/DATA-A#(.*)#/$1/;
may not do what you actually want. It removes "DATA-A#" and the final "#" from the line. So, for instance, given the line
aasdfDATA-A#item1a#DATA-B#item2b#
from your sample data, it would produce
aasdfitem1a#DATA-B#item2b
Assuming you're trying to extract the text "item1a" from the line, the regex you want is
$line =~ /DATA-A#([^#]*)#/;
which extracts "item1a" into $1 (without destroying the rest of the line, so the DATA-B will still be there to collect later). Using [^#]* instead of .* will cause it to stop capturing at the first # it sees instead of continuing to the last one.

I suppose something like

my %data = (); while ($line =~ /(TITLE|DATA-[ABC])#([^#]*)#/g) { $data{$1} = $2; handle_data($data{TITLE}, $data{DATA-A}, $data{DATA-B}, $data{DATA-C}) if $1 eq 'DATA-C'; }
might be what you're looking for here, but I'm not entirely sure. Note the assumption that you identify the end of a data set by the appearance of a DATA-C element. handle_data would then either print the data, store it for later formatting, or whatever else may need to be done with it. Other initialization and/or sanity checking is probably needed unless your input stream is known to be perfect and will never, say, send an ITEM-C before all of the other elements have appeared.

update: Added a forgotten ) in the last code fragment. This code is (obviously) untested. If it breaks, you get to keep both pieces.


In reply to Re^3: Looking for Perl Elegance! by dsheroh
in thread Looking for Perl Elegance! by perlNinny

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.