Welcome to the world of programming, which is much better learned by doing than by hearing. Although of course you've got to start somewhere.
- I suggest going over the tutorials on this site.
- Work out the algorithm as steps in English, not necessarily as a program. How would you do it without a computer?
- Work out how to do what you describe in Perl.
Post what you have, both steps and code, and we can guide you.