Example:
Lets say I have the chemical formula
CuSO4 + KCl = CuCl2 + K2SO4
I want to be able to split the formula into different parts based on their elements. I'll be inserting them into a table myself, but I'll use the end result of the table to show you what I want.
reactant1 = {Cu = 1, S = 1, O = 4}
reactant2 = {K = 1, Cl = 1}
product1 = {Cu = 1, Cl = 2}
product2 = {K = 2, S = 1, O = 4}
If you know chemistry, you can tell that this equation isn't balanced, and even though it would be easy for me to do, I'd like to be able to program it as well, (I can probably do that on my own, just need the splitting)
Now for my code currently,
local function findChem(str)
for word in string.gmatch(str,'%u%l%u*%d*') do
print(word)
end
end
findChem("CuSO4")
Using this it WILL print out CuSO4, however that's on one line, how do I get it so that Cu is on one line S on another O on the next, and 4 on the last??