If you return an array in scalar context (as opposed to a list context) you get the number of elements in the array.
So I suspect that is what you doing either in debugging or the actual code. I would probably try splitting the code out into simpler more explicit instructions.