I would assume that you haven't posted the code in its entirety, since the variable 'mylist' isn't initialised in the code, furthermore, the file descriptor that is apparently bound to the variable 'mylist' isn't closed.
In your final foreach expression, note that it will iterate over every item in the list 'EndList', but will only print the first item [(nth 0 endlist)] in each iteration.