my $last; while() { if(/^\s+(spg-risk\S+)/) { $items{$1} = $_; $last = $1; } elsif($last) { $items{$last} .= $_; } else { warn "Extended line found before any items found\n"; } }