A slightly different take than pg's, using references:

my %hash; my $buf; local $_; while (<$fh>) { chomp; if (my ($key) = /^>(\w+)/) { $hash{$key} = ''; $buf = \$hash{$key}; } else { $$buf .= $_; } }
The key difference is that you assign the value before you've filled it up rather than using a temp variable and assigning it after. This means you only have to do assignment to the hash once (getting rid of that last special assignment outside the loop), and you don't need to copy the (potentially large) value. There's no real reason for using a reference to the value rather than keeping the key around and using $hash{$key} instead of $$buf - it just came out that way and it feels nice because I've isolated the hash business to one place. It well expresses how I thought about the problem. The loop isn't about the hash really, it's about buffering up data spread over different lines. The data could just as well have been stored in an array.

ihb

Read argumentation in its context!


In reply to Re: Concatenating text for a hash problem by ihb
in thread Concatenating text for a hash problem by monkfan

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.