in reply to Insert newline
maybe something like this
# open file as usual # foreach line of the file # DON'T CHOMP, (still) if (/\(/) { # I have found a "(", this must be a title, so I: chomp; push @titles, $_; # now this is the more problematic part cause you need # to extend your search to several lines # but you are processing line by line, the idea is that # we have a title, now we stop and parse text. # 1- pick up all between ) and ( and save this to a $var # ie: with \).*?\( # unfinished yet, take a look to substr # 2 -split the different items in this text-block @texts = split /\n/, $var; pop @texts # the last value is the next title, # so we can discard it safely # we enter a inner loop for all items in the array while (@texts) {print $titles[-1] . '; '. $_} } else { next # this is not a title, whe forget this line } # end of the if-else block } # end of the foreach-block
This code is not complete, only a rough draft showing the idea: you have two problems here, 1) - chase the titles one by one (this should be very easy) and 2) - to enter a "inner parse mode" after a title was found, stop the hunting and process all text until the next title line"
Update: you need to wide the @texts array after each foreach pass, so insert a @texts = (); line just after # DON'T CHOMP
|
|---|