L
Luotao Fu
Hi All,
My first post on this Groups, so sorry for any possible stupidity
I'm wrting since days a perl programm. The programm contains a small
routine, wich shall parse a chemical formal and return the name and
portion of single atoms
in the material as a array(or a hash) Well my code looks like that:
my @literals=split /([A-Z])/, $molecule;
for (my $i=0; $i<=$#literals; $i++){
my @atom;
print "Literal: ", $literals[$i], "\n";
push(@atom, $literals[$i]);
if ($literals[$i+1] !~ /[A-Z]/){
push(@atom,$literals[$i+1]);
$i++;
}
push(@atoms,join("",@atom)};
}
The $molecule contains the formal (i.E. H2O, FeCl3 or CaCl), Every Beginning
letter of a element ist written in upper case. As you can see, I split
first the $molecule with Letters in upper case, which means FeCl3
turns into {F,e,C,l3}, than I scan the splitted list, which is stored
in the array @Literal, for capital
letters, every capital letter will be pushed in a temporary Array. If
the following item in array is not written in upper case, which means, that
the Name of the atom contains more than one letter, it'll be also pushed in
the same temporary Array, which will be later joined and puted in the
output array. The final result of the Formal H20 should be {H2,O},
FeCl3 {Fe,Cl3} and so on....
This works so far, but I'm far not satified with this solution. There
must be better ways to solve it. which more intelligent RegExp and so
on. But I'm not quite familiar to RegExps in Perl, so that I can't think
out any better solution.
Anyone Idea, how I can write this routine more elegantly?
Thanx A lot
Cheers
Luotao Fu
My first post on this Groups, so sorry for any possible stupidity
I'm wrting since days a perl programm. The programm contains a small
routine, wich shall parse a chemical formal and return the name and
portion of single atoms
in the material as a array(or a hash) Well my code looks like that:
my @literals=split /([A-Z])/, $molecule;
for (my $i=0; $i<=$#literals; $i++){
my @atom;
print "Literal: ", $literals[$i], "\n";
push(@atom, $literals[$i]);
if ($literals[$i+1] !~ /[A-Z]/){
push(@atom,$literals[$i+1]);
$i++;
}
push(@atoms,join("",@atom)};
}
The $molecule contains the formal (i.E. H2O, FeCl3 or CaCl), Every Beginning
letter of a element ist written in upper case. As you can see, I split
first the $molecule with Letters in upper case, which means FeCl3
turns into {F,e,C,l3}, than I scan the splitted list, which is stored
in the array @Literal, for capital
letters, every capital letter will be pushed in a temporary Array. If
the following item in array is not written in upper case, which means, that
the Name of the atom contains more than one letter, it'll be also pushed in
the same temporary Array, which will be later joined and puted in the
output array. The final result of the Formal H20 should be {H2,O},
FeCl3 {Fe,Cl3} and so on....
This works so far, but I'm far not satified with this solution. There
must be better ways to solve it. which more intelligent RegExp and so
on. But I'm not quite familiar to RegExps in Perl, so that I can't think
out any better solution.
Anyone Idea, how I can write this routine more elegantly?
Thanx A lot
Cheers
Luotao Fu