That is really ugly code. Here is short and very generic solution:

my %hash; my $current_token = ''; while (<DATA>) { my ($token, $value ) = $_ =~ m/^([^\s]+)\s+(.*)/; next unless $token; if ( $token eq $current_token ) { ${$hash{$token}}[-1] .= ' ' . $value; } else { $current_token = $token; push @{$hash{$token}}, $value; } } use Data::Dumper; print Dumper \%hash; __DATA__ HEADER Header 1 stuff TITLE Title 1 stuff TITLE 2 more title 1 stuff COMPND complicated stuff 1 COMPND 2 continued complicated stuff 1 HEADER Header 2 stuff TITLE Title 2 stuff TITLE 2 more title 2 stuff COMPND complicated stuff 2 COMPND 2 continued complicated stuff 2 HEADER Header 3 stuff TITLE Title 3 stuff TITLE 2 more title 3 stuff COMPND complicated stuff 3 COMPND 2 continued complicated stuff 3 COMPND 3 continued complicated stuff 3 HEADER Header 4 stuff TITLE Title 4 stuff TITLE 2 more title 4 stuff COMPND complicated stuff 4 COMPND 2 continued complicated stuff 4 HEADER Header 5 stuff TITLE Title 5 stuff TITLE 2 more title 5 stuff COMPND complicated stuff 5 COMPND 2 continued complicated stuff 5 HEADER Header 6 stuff TITLE Title 6 stuff TITLE 2 more title 6 stuff COMPND complicated stuff 6 COMPND 2 continued complicated stuff 6 __END__ $VAR1 = { 'HEADER' => [ 'Header 1 stuff', 'Header 2 stuff', 'Header 3 stuff', 'Header 4 stuff', 'Header 5 stuff', 'Header 6 stuff' ], 'TITLE' => [ 'Title 1 stuff 2 more title 1 stuff', 'Title 2 stuff 2 more title 2 stuff', 'Title 3 stuff 2 more title 3 stuff', 'Title 4 stuff 2 more title 4 stuff', 'Title 5 stuff 2 more title 5 stuff', 'Title 6 stuff 2 more title 6 stuff' ], 'COMPND' => [ 'complicated stuff 1 2 continued complicated s +tuff 1', 'complicated stuff 2 2 continued complicated s +tuff 2', 'complicated stuff 3 2 continued complicated s +tuff 3 3 continued complicated stuff 3', 'complicated stuff 4 2 continued complicated s +tuff 4', 'complicated stuff 5 2 continued complicated s +tuff 5', 'complicated stuff 6 2 continued complicated s +tuff 6' ] };

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: An (almost) useful idiom; needs work. by tachyon
in thread An (almost) useful idiom; needs work. by BrowserUk

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.