shorter way to write this loop?

it works but i can’t help but thinking a real programmer would write this in a few lines…
so what it does is compare every current element to next inline. it has to join elements if they are UpperCase followed by LowerCase and if two digits follow up…
i need to translate stuff like NaAlSi3O3 to Na,AL,SI,3,O,3 or C8H15O2 to C,8,H,15,O,2

var formula : String;
var atomList =new Array();

function Start ()
{
	var i=0;	
	while (i<formula.Length-1)
	{
		print(i);
		print(formula.Length);
		if(i==formula.Length-2  System.Char.IsDigit(formula[i])  System.Char.IsLetter(formula[i+1]))
		{
			var amount=parseInt(formula[i].ToString());
			atomList.Add(amount);
			var name=formula[i+1].ToString();
			atomList.Add(name);
		}
		else if(i==formula.Length-2  System.Char.IsLetter(formula[i])  System.Char.IsDigit(formula[i+1]))
		{
			name=formula[i].ToString();
			atomList.Add(name);
			amount=parseInt(formula[i+1].ToString());
			atomList.Add(amount);
		}
		else if(i==formula.Length-2  System.Char.IsUpper(formula[i])  System.Char.IsUpper(formula[i+1]))
		{
			name=formula[i].ToString();
			atomList.Add(name);
			name=formula[i+1].ToString();
			atomList.Add(name);
		}
		else if(i==formula.Length-2  System.Char.IsUpper(formula[i])  System.Char.IsLower(formula[i+1]))
		{
			name=formula[i].ToString()+formula[i+1].ToString();
			atomList.Add(name);
		}
		else if(i==formula.Length-2  System.Char.IsLower(formula[i]))
		{
			name=formula[i].ToString();
		}
		else if(System.Char.IsUpper(formula[i])  System.Char.IsLower (formula[i+1]))
		{
	    	name=formula[i].ToString()+""+formula[i+1].ToString();
	        atomList.Add(name);
		}
		else if(System.Char.IsUpper(formula[i]) System.Char.IsUpper(formula[i+1]))
		{
			name=formula[i].ToString();
			atomList.Add(name);
		}
		else if(System.Char.IsUpper(formula[i])  System.Char.IsDigit(formula[i+1]))
		{	
			name=formula[i].ToString();
			atomList.Add(name);
		}
		else if(System.Char.IsLower(formula[i]) System.Char.IsUpper(formula[i+1]) || System.Char.IsLower(formula[i]) System.Char.IsDigit(formula[i+1]))
		{
			name=formula[i].ToString();
		}
		else if(System.Char.IsDigit(formula[i])  System.Char.IsDigit(formula[i+1]))
		{
			amount=parseInt(formula[i].ToString()+formula[i+1].ToString());
			atomList.Add(amount);
		}
		else if(System.Char.IsDigit(formula[i])  System.Char.IsUpper(formula[i+1]) !System.Char.IsDigit(formula[i-1]))
		{
			amount=parseInt(formula[i].ToString());
			atomList.Add(amount);
		}
		else if(System.Char.IsDigit (formula[i])  System.Char.IsDigit(formula[i-1]))
		{
			amount=parseInt(formula[i].ToString()+formula[i-1].ToString());
		}
	i++;
	}
	print(atomList);
}

I didn’t read all of the code, but whenever I have a bunch of else if statements put together, I usually try to make it into a switch case. Makes things a bit simpler, and easier to read.

Example:

It would probably be easier if you were to use regular expressions instead of parsing the string manually. I have never used regular expression in JavaScript/UnityScript before, but I think the following code should work for you:

var formula = "C6H12O6";
var matches = formula.match(/^([A-Z][a-z]*\d*)+$/g);

// matches = ["C6", "H12", "O6"]

The key part is the “^([A-Z][a-z]\d)+$” string, which is a regular expression that will match a single capital letter followed by an optional small letter followed by zero or more digits.

Give it a try and tell me how it goes.

Thanks I will give both a try when i m home
about the .match method, can i make different .matches in an if statement?
Edit: read the regular expressions link, all i need to know thanks

hey shaderop thanks again got it to work
only problem i have is my function keeps running or at least adding the same result to my array…? any idea why that is?

import System.Text.RegularExpressions;
import System;

static var AtomList=new Array();

static function ReadString (formula:String)
{	
	var matches ="([A-Z]?[A-Za-z])+";
	var output=Regex.Split(formula, matches);
	for(var i=1; i<output.Length-1; i+=2)
	{	
		var name=output[i];
		var amount=output[i+1];
		if(amount=="")amount="1";
		AtomList.Add(name);
		AtomList.Add(amount);
		ReadLetter(name);
	}	
}

static function ReadLetter (L:String)
{	
        //get the electron nr on outer shell if element is a non metal.	
	var ElectronNr:int;
	var e=1;
	var collums=["H","Li","Na","K","Rb","Cs","Fr",
	"-","Be","Mg","Ca","Sr","Ba","Ra","-","B","-","C","Si",
	"-","N","P","As","-","O","S","Se","Te","-","F","Cl",
	"Br","I","At","-","He","Ne","Ar","Kr","Xe","Rn"];
	
	for(var j=0; j<collums.Length; j++)
	{
		if(collums[j]=="-")e++;
		if(L==collums[j])ElectronNr=e;
	}
	AtomList.Add(ElectronNr);
}

this is how i call it in an other script

function OnGUI ()
{
	GUILayout.BeginArea(Rect((Screen.width/2)+(Screen.width/12), 10,Screen.width/3,Screen.height/3));
	if(selected)
	{	
		GUILayout.Label("splitting container");	
		GUILayout.BeginVertical();	
		scrollPosition=GUILayout.BeginScrollView (scrollPosition);
		print(content);
		for(var i=0; i< content.length; i+=2)
		{	
			matching.ReadString(content[i]);//Here is where i use it.
			print(matching.AtomList);
		}
		for(var n=0; n< matching.AtomList.length; n+=3)
		{
			var Atom:String=matching.AtomList[n];
			var AMT=matching.AtomList[n+1];
			var El:int=matching.AtomList[n+2];
			GUILayout.BeginHorizontal();
			GUILayout.Box(Atom);
			GUILayout.Box(AMT.ToString());
			GUILayout.Box(El.ToString());
			GUILayout.EndHorizontal();
		}
		GUILayout.EndScrollView();
		GUILayout.EndVertical();
	}
	GUILayout.EndArea();
}

auwtch never mind its because my array content keeps running result out of the for loop in the second script. i removed it after converting and now it works just fine! :slight_smile: