my %result = (); my $command = ''; for my $line (@d) { if ($line =~ /^<(.+)/) { $command = $1; } $result{$command} .= $line; }