I threw your code into my editor and threw in use strict. Most looks fine, but the %catReplace hash doesn't get initialized anywhere I can see - would this matter? The other obvious possibility is that you've got whitespace problems. Have you tried seeing if length returns the expected values?