I'm sorry - I completely missed that you are already using a hash. I did not read your code closely enough and thought you were using two loops. So you were quite close, and just missed the step from using the keys to remember what things you've seen to also using the values to remember the original format.