in reply to What is the problem with this script as no output

You're probably not clearing any all of the intermediate variables in your loop.

I'd advice you to use strict and declare the variables in the right scope with my. that should clear up most if not all of the problems.

Update: missed the undef statement. using strict is still a better idea, though.