It's alright, I got it figured out.
I'd deleted my code several times in frustration before I posted here..
Basically my problem was that I was trying to do a while loop BEFORE it entered into the if statements based on what $1 was. I moved it inside the if ($1 == begin) section and everything fell into place.. :-)
Now if only I could get it to put a : in the menu item.. $chr(58) doesn't work, still uses it as the : seperating the item name from the command