What would the appropriate syntax be in the code below in reference to declaring a variable. $ref will never be used outside the loop and needs to be reset every iteration of a loop. Do I initialise outside the loop, reinitialise every time within the loop or is there a different way to do it.

If $ref is only needed inside the loop and needs to be reset for each iteration, then it should be declared inside the loop. If it's not going to be reset, then it should be declared outside of the loop.

Since, in your example, $ref is just a copy of $i, do you really need that second var?

On another note, using the C style for loop is messy. It would be cleaner to use the perl style for loop.