in reply to Running a subroutine a certain number of times

Your $i isn't lexical. Hence, the $i you are using in the outer loop, is the same $i you are using in the subroutine. Hence, after exiting the subroutine, $i == $leg, which is probably equal or larger than $number_of_sequences.

Use lexical loop variables that are local to just the loop. And for the first loop, you need to increment $i.

Abigail