This can also be accomplished without a regex at all, thanks to the split() function.
use strict;
use warnings;
use Data::Dumper;
my %hash;
my $header;
while (my $line = <DATA>) {
chomp $line;
my ($k, $v) = split ':', $line;
next if ! $k;
if (! $v) {
$header = $k;
next;
}
$hash{$header}{$k} = $v;
}
print Dumper \%hash;
__DATA__
first:
this:that
here:there
when:what
how:where
now:later
second:
this:that
here:there
when:what
how:where
now:later
Output:
$VAR1 = {
'second' => {
'now' => 'later',
'how' => 'where',
'here' => 'there',
'this' => 'that',
'when' => 'what'
},
'first' => {
'when' => 'what',
'how' => 'where',
'here' => 'there',
'this' => 'that',
'now' => 'later'
}
};