sprintf is just composing a new string that has the extracted number on the front, padded to five spaces with leading zeroes. Substr is taking that back off.
The uninitialized warning suggests that the pattern match isn't successful. Could you provide some of the data you've got, so we can reproduce the warnings?