If using 'my' on some of the variables fixed it, you probably had a locality problem. Were $horz and $vert populated somewhere else in the script before you tried to use them?
Without seeing the whole script, my guess would be that something somewhere else was changing the values of $horz and $vert before you used them but after you thought you'd set them.