in reply to Concatenating text for a hash problem

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!