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";
@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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||