Yep, that is correct. The logic is really no different than how you had it. But if you're going to use arrays, you might as well take full advantage of the built-in functionality designed for them.

Btw, as NewJob pointed out, you were not resetting the value of $a, which in turn was probably hosing up your substr. I also changed the while loop to check @error (returned by the readline function) as opposed to checking for an empty string.