Ver nice response Limbic, my apologies for being unclear. I've replied to
BrowserUK above and hope the explanation sheds some light on my problem.
The points you've made above are exactly what I've intended the code to do.
I have checked the results as you have suggested by using print, what seems to happen is that the result of $key_num seems to stay the same and it works for the last iteration of the loop, the last key number in %columns is always incremented by one, though it seems that no matter how many times I loop, the last inserted key is overwritten, seems to me (I used print) that $key_num always returns the same number.
BlackJudas