Take pity on ikegami. Don't write Perl code to perform this task. Write out how you would solve the problem (step by step) in English. Don't talk about hashes or loops. Just describe how you would solve the problem if you had to do it manually.
That should give you an insight into how to code a solution.