- Turn the above into a subroutine, passing the subscript of $name_breakdown you wish to test (rather than 1).
- Modify the code to use the parameter, rather than hardcoding 1, 2, and 3.
- Return whether or not not the replacement took place.
Call the routine with a parameter of 1. If it does not do the replace, call it with a parameter of 2.
(If you're really ambitious, store the name prefaces in a hash, and use the hash as a test. Sooner or later, the list will change...)