bobn's code is exactly what I was looking for thankyou.
Sorry to confuse people with the code in my while loop, it does exactly what I want it to do although it has correctly been pointed out that it is messy. In response to a specific query, the chop is used to get rid of a semicolon that is always at the end of the pattern.
The else part is unfaulty code but does not do what I want it to do.
Many thanks.