my @list; #Read first, ask questions later is probably not optimal for this #Scope our variable, merge, read, and assign all at once while(chomp(my $line = )){ #Save a match, substitution returns true iff we susbtituted if ($line =~ s/^\+//){ #Append to previous line (last line in the array) $list[-1] .= $line; } else{ #Save the line push(@list, $line); } } #Map is good, nice way to get all the newlines back #join would work if you the file need not end in a newline print map {"$_\n"} @list; #### my @list; while(chomp(my $line = )){ #Trinary is good ;-) $line =~ s/^\+// ? $list[-1] .= $line : push(@list, $line); } print map {"$_\n"} @list; #### my @list; #for is very perlish, so is using $_ chomp && $_ =~ s/^\+// ? $list[-1] .= $_ : push(@list, $_) for ; print map {"$_\n"} @list;