Right you are, my apologies. The ex values that are not "ex1" (ex1 is produced at a mismatch in the comparison) revert to the default value of ex# (for example, ex18 or ex30) instead of counting up from 1. Basically, what I think is happening is ex1 is not passed on to the else part, since ex values get clobbered after one iteration of the loop. I've been trying variations of an ex variable external to the loop, but can't seem to figure it out.
I'm still having trouble understanding what you mean. Please post your code, along with about a dozen lines of your input data that demonstrate the problem, as well as what you would like the output from those lines to be.
Available for small or large Perl jobs; see my home node.