I recall I made something similar to this with hash in the old days. I'll help you, but I can't do it now, you don't mind if I send you for it tomorrow?
Or I'll just give the main points :
Create a group, then whenever the unit takes damage from the spell, add them to this exclusion group. Whenever you loop, check if it's in the exclusion group, if they're there, ignore them else damage and register them.