in reply to Why does a full iteration over array & shift is not emptying the array

Modifying the array you're iterating over is a bad idea.

The explanation here is that $x is bound to the first array item (1) in the first iteration, and to the second array item (which is then 3) in the second iteration. At that time there's no third item in the array anymore, and the body of the loop is executed only twice (as you can see from your debugging output).

It becomes a bit clearer if you also print out $x in the loop body.