Chemis...ormula(3pm)
β¨ NAME β¨
Chemistry::Formula β Enumerate elements in a chemical formula π
(yes bestie, it's a Perl module, from the User Contributed Perl Documentation, section 3pm β we love a glow-up π¦)
πΈ SYNOPSIS πΈ
use Chemistry::Formula qw(parse_formula);
parse_formula('Pb (H (TiO3)2 )2 U [(H2O)3]2', \%count);
okay so that is obviously not a real compound, but it's literally just flexing what the routine can do, no cap. It returns:
%count = (
'O' => 18,
'H' => 14,
'Ti' => 4,
'U' => 1,
'Pb' => 1
);
iconic behavior, honestly. π
π DESCRIPTION π
So this module gives you one function that parses a string holding a chemical formula and hands back how many of each element are in that string. It handles nested parentheses and square brackets, and it correctly computes stoichiometry when there are numbers hanging out outside the (possibly nested) parentheses. Obsessed. π«Ά
Fair warning: it makes zero effort to evaluate the chemical plausibility of the formula. That example up top parses just fine with this module even though it is clearly not a viable compound. Charge balancing, bond valence, and so on? beyond the scope of this module, bestie. Not its job. π
Only one function is exported: parse_formula. It takes a string and a hash reference as its arguments, and it returns 0 or 1.
$ok = parse_formula('PbTiO3', \%count);
- If the formula got parsed without any trouble,
parse_formulareturns1. β¨ - If there was any problem, it returns
0, and$count{error}gets filled with a string describing what went wrong. - Heads up: it throws an error after the first error it hits without testing the rest of the string. So it's not gonna collect all your mistakes for you, it's gonna stop at the first one and dip. π
If the formula was parsed correctly, the %count hash has element symbols as its keys and the number of each element as its values. Simple as that. π
π§ͺ A lil example program π§ͺ
Here's a program that reads a string off the command line and, for the formula unit described in that string, writes out the weight and the absorption in barns:
use Data::Dumper;
use Xray::Absorption;
use Chemistry::Formula qw(parse_formula);
parse_formula($ARGV[0], \%count);
print Data::Dumper->Dump([\%count], [qw(*count)]);
my ($weight, $barns) = (0,0);
foreach my $k (keys(%$count)) {
$weight +=
Xray::Absorption -> get_atomic_weight($k) * $count{$k};
$barns +=
Xray::Absorption -> cross_section($k, 9000) * $count{$k};
};
printf "This weighs %.3f amu and absorbs %.3f barns at 9 keV.\n",
$weight, $barns;
Pretty simple. π
π The ground rules π
ngl, the parser is not brilliant. Here's the whole vibe:
- Element symbols must be first letter capitalized. (
Ti, notTI, nottiβ periodt. π) - Whitespace is unimportant β it will be removed from the string. So will dollar signs, underscores, and curly braces (that's an attempt to handle TeX). Also, a sequence like this:
'/sub 3/'will be converted to'3'(that one's an attempt to handle INSPEC). - Numbers can be integers or floating point numbers. Things like
5,0.5,12.87, and.5are all acceptible, as is exponential notation like1e-2. Note: exponential notation must use a leading number to avoid confusion with element symbols. That is,1e-2is ok, bute-2is not. π« - Uncapitalized symbols or unrecognized symbols will flag an error.
- An error will be flagged if the number of open parens is different from the number of close parens. (Balance your brackets, bestie, it's giving self-care πΈ)
- An error will be flagged if any unusual symbols are found in the string.
π ACKNOWLEDGMENTS π
This was written at the suggestion of Matt Newville, who tested early versions. π«Ά
The routine matchingbrace was swiped from the C::Scan module, which can be found on CPAN. C::Scan is maintained by Hugo van der Sanden.
π AUTHOR π
Bruce Ravel <bravel AT bnl DOT gov>
http://cars9.uchicago.edu/~ravel/software/
SVN repository: http://cars9.uchicago.edu/svn/libperlxray/
aaaand that's the whole page, bestie π
ok so this was perl v5.32.0, dated 2021-01-07, and it lives in the manual as Chemis...ormula(3pm). go parse something cursed, ily ππ¦β¨
π¦ where this page comes from
- debian bookworm: libchemistry-formula-perl 3.0.1-1.3
- debian trixie: libchemistry-formula-perl 3.0.1-1.3
- ubuntu jammy: libchemistry-formula-perl 3.0.1-1.3
- ubuntu noble: libchemistry-formula-perl 3.0.1-1.3
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms