R3volution has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Assistance please
by Kenosis (Priest) on Nov 21, 2012 at 20:57 UTC | |
Re: Assistance please
by grondilu (Friar) on Nov 21, 2012 at 21:46 UTC | |
Re: Assistance please
by Don Coyote (Hermit) on Nov 21, 2012 at 22:07 UTC |