http://www.perlmonks.org?node_id=1005960

perlguru22 has asked for the wisdom of the Perl Monks concerning the following question:

I have this following assignment to do. "Write a program that prompts the user for a chemical formula and returns the molecular weight. For example H2O would return 18.01. Use the weights file as your source file for the symbols and their weights. Be sure to include a loop so the user doesn't have to reenter the program to look up a weight. Here's another example: The weight of C12H22O11 (table sugar) is found by looking up the weights of each element and multiplying them by the number of their occurrences and adding them all together. At this level don't worry about parentheses, e.g., Ca(OH)2. However, for an A+ try it after you can calculate weights without parantheses." I've figured out most part of it
Print "enter a formula.\n"; chomp ($form=<STDIN>); @array = $form =~ /[A-Z][a-z]?|\d+/ig
So if the user inputs H22011C12 it would give me H 22 0 11 C 12 all seperate so my next step is to pass that array into a subroutine. I know I have to multiply the weight of the symbol by the occurrence so if the weight of H is 12.01 I multiply by 22 and so on than I add all the totals I just can't seem to figure out how to start my subroutine I have a weights file goes with this. I was just hoping so one could give me some guidence or explain the process to me Thank you so much.