Well Nivethan, logic wise, it seems that we are both on the same page for both parts of the puzzle.
My code is the same number of lines, but I straight lined it, while you uses of GOSUBS works as well.
One difference, I prefer dynamic arrays for a puzzle like this. I only use dimension arrays, when I need them to hold something like records or even file pointers. It just seems easier.
Also, I'm working on a system with QM. So I don't know if the functionality of TRIM is the same on other DB's?