Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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.

Comment on Formula program
Download Code
Re: Formula program
by mbethke (Hermit) on Nov 28, 2012 at 06:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005960]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-10-01 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls