in reply to Textfile to csv with a small twist

Parsing with a state variable ($category in this case) is one way to remember which heading th text falls under:
use Data::Dumper; use strict; use warnings; my %parse_tree; my $category; while (my $line = <DATA>) { if ($line =~ /^(\w+:)$/) { $category = $1; $parse_tree{ $category} = []; } else { push @{$parse_tree{ $category}}, $line; } } print Dumper( \%parse_tree); __DATA__ heading1: text1 text2 text3 heading2: text4 text5 text6
yields
$VAR1 = { 'heading1' => [ 'text1 ', 'text2 ', 'text3 ' ], 'heading2' => [ 'text4 ', 'text5 ', 'text6 ' ] };
In the dumped hash, note that the newlines are preserved.

Update: altered the regex to capture the colon.

-Mark