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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.