in reply to match tags

Create a variable (lets call it $collect), where you concatenate any BDs. If you find anything else, print and clear $collect. If that 'anything else' is AU, initialize $collect with it. In essence:
if ($data[$i]=~ m/^\.\.BD/) { unless ... { $collect .= ',' . $data[i+1]; } } else { print $collect,"\n" if ($collect); $collect=''; if ($data[$i]=~ m/^\.\.AU/) { unless ... $collect= $data[i+1]; } } } }

You should be able to adapt the rest.

Note that you would need to add a g modifier behind s/^\s+|\s+$// so that both leading and trailing spaces can be removed in the same string, otherwise it would remove only the first one it finds

If you need error detection (because there might be files that have no AU before a BD), you can test that too. If you find a BD while $collect is empty, that would be the condition to print an error message

Also you don't have any use warnings; and use strict; in your script. While not necessary for a working program those lines are heavily recommended. And you will hear that advice every time you post something here unless you add them