I know you've gotten lots of answers, but (without getting into what you were trying to do) couldn't the "goto xyz" be replaced with "last"? That should take you to the end of the "foreach $j ...." loop and back to the "foreach $i" loop. Then get rid of the xyz label?
-- Carmen