perlquestion
R3volution
<p>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:
</p>
<code>#!/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 {
}
</code>
<p>
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.
</p>