I'm having a hard time trying to get the concept of a bit of code to work. Presently I have 2 files, for the sake of ease, File A and File B. File A contains headings then information below in regards to the heading, for example:

1,a heading, some more information
Stuff stuff stuff
Some more stuff more stuff
2, a second heading, even more information
Stuff stuff

File B contains lines in pairs, one line of the pair is the headings found in File A, the second line is information linked to the first.
I have written (in hast mind you) a small script to search File A and B. If the script finds a matching heading in both files I'd like it to write out the pair of accossiated lines in File B followed by the accossiated information in File A, only write out the lines in File A up until the next heading. This should loop through until the end of File A is reached.
Not having had much success I ask for some help with this approach
Many thanks in advance,
Tom