Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi, I am very new to perl and am having issues with a program that will allow me to calculate Chemical Formulas. I am trying to create a sub-routine to do this. So far, I have:

#!/usr/bin/perl while ($lines = <>) { chomp ($lines); (undef, $symbols, $weights) = split /,/, $lines; $weights{$key} = $weights; } print "Please enter a Chemical Formula: "; chomp ($formula = <>); @array = $formula =~ /[A-Z][a-z]?|\d+/g; sub mol_form { }

This code works as far as splitting a Formula. But I am trying to use a subroutine so that the script will properly calculate the formula. I have tried several different ways and nothing seems to work. Basically for example, If I have H2O, it will multiple the molecular weight of Hydrogen by 2 and then add that to the molecular weight Oxygen. I'm thinking I may need 2 sets of IF statements in my subroutine to get the calculation right. Of course, if there are no numbers, it will just add the molecular weights themselves. It's probablty really easy to do and I am looking too much into it. Any help would be appreciated.

In reply to Assistance please by R3volution

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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:
    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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2018-04-24 23:54 GMT
    Find Nodes?
      Voting Booth?