in reply to 'Invisible' newlines/line wrapping

Update: OP updated as this was written. Original (or maybe second version of 3?) was actually better. In any case, my refs to lines 2 and 5 should now be read as 5 and 16&27 (I think).</update>

Your input, $index=<STDIN>; at line 2 is going to have a trailing newline (think of the return you hit to terminate the input, if that notion is helpful).

So, to have line 5 print without an inadvertent newline, make line 3:
chomp $index;
... and your problem should be solved.

Now, we know this is just a snippet, but FTR, one hopes that you're using strict and warnings which will actually help you avoid or understand many other apparently odd behaviors.

And, welcome to the Monastery!