Sorry, I thought I previously had this working, but trying this morning, I can't get it to work. Hmm... maybe I was thinking about a different programming language.
Anyway, I did come up with a sort of work-around. If you want to have a look at it, the key is in the screenshot.