Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Formula program

by perlguru22 (Acolyte)
on Nov 28, 2012 at 05:19 UTC ( #1005960=perlquestion: print w/replies, xml ) Need Help??
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.

Replies are listed 'Best First'.
Re: Formula program
by mbethke (Hermit) on Nov 28, 2012 at 06:15 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005960]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-27 16:10 GMT
Find Nodes?
    Voting Booth?