I completely agree with you that your code is much better than mine! The problem is that I made mine before I saw your post, and also, I'm new enough with perl that I don't really understand everything that's going on in yours. Do you mind adding some comments for the newbie?
Thank you for the help!