in reply to number of keys and values in a hash

Your code has two opening curly brackets, but three closing ones. Please, post the real code, otherwise we can not help you.
Are you sure you are not calling keys inside the each loop in the real code? That would explain the infinite loop.