in reply to Re^2: Looking for Perl Elegance!
in thread Looking for Perl Elegance!

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.