Any time you have repeated copies of similar code, and variable names that contain a sequential integer, it should be a clue that you really have a data structure. It seems that what you want is an array (or perhaps a hash) above your other hashes so that you have one structure over which you can iterate.