read char in string and check if number or letter.

whats wrong with this, i need to be able to decode formula input to usable data, ints strings. but I’m having trouble switching form string to char…

var formula:String;
var reader=new Array(char);
public var atomArray=new Array(setValues);
var chr:char;
public class setValues
{
	var AtomName:String;
	var AtomAmount:int;
	//var AtomElectrons:int;
}

function Start ()
{		
	for(var i=0; i<formula.Length; i++)
	{
		reader[i]=formula.Substring(i,1);
		chr=reader[i];
		if (chr.IsLetter)//chr > 'A' || chr < 'Z')
		{
	 		var name=chr;
	 	}
	 	if (chr.IsDigit)//chr > '0' || chr < '9')
	 	{
	 		var amount=chr;
	 	}
	 	atomArray.Add(name,amount);
	 	print(reader);
	 }
}

Several things…you shouldn’t use Array, you can’t use Array with a type like that anyway (use List), you should use the index of a string to get a char rather than Substring, you need to convert the chars to String or int as needed, and that’s not how you use IsLetter or IsDigit. I’m not really sure what you intend as far as “name” and “amount” goes since they both should still be defined whether the char is a letter or digit, and you should probably have some contingency if the char is neither a letter nor a digit. Also variables should be local where possible, and preferably you’d use uppercase for class names and lowercase for variable names. Making a constructor for SetValues would make it easier to use. (Plus I’d suggest a more appropriate name than SetValues, which sounds like a function name rather than a class name. Shouldn’t “atomName” be a char rather than a String?)

import System.Collections.Generic;

var formula : String;
var reader : List.<char>;
var atomList : List.<SetValues>;

class SetValues
{
    var atomName : String;
    var atomAmount : int;
    //var AtomElectrons:int;
    function SetValues (atomName : String, atomAmount : int) {
    	this.atomName = atomName;
    	this.atomAmount = atomAmount;
    }
}

function Start ()
{
	reader = new List.<char>();
	atomList = new List.<SetValues>();
	
    for (var i = 0; i < formula.Length; i++)
    {
        var chr = formula[i];
        reader.Add (chr);
        if (System.Char.IsLetter (chr)) //chr > 'A' || chr < 'Z')
        {
            var name : String = chr.ToString();
            var amount : int = 0;
        }
        if (System.Char.IsDigit (chr)) //chr > '0' || chr < '9')
        {
        	name = "";
            amount = parseInt (chr.ToString());
        }
        atomList.Add (new SetValues(name, amount));
        Debug.Log (reader[i]);
     }
}

–Eric

hey thanks Eric, so why is it not good to use these kind of array? are they so slow or inefficient?
name is a string because i need to read chemical formula like H2O, and since some elements have two chars in their name… like Br

and why do i need the setValue function? if the output from the for loop just gets added to the list isn’t that enough?
the idea is that i can give GO’s Formula names, the player can “mine” from these GO’s and collect molecule. These molecules are the formula as a string. so i need the computer to be able to read the molecules structure and know how many atoms of H are in H20, how many O’s and how many electrons they both have (still trying to figure out where to get the electron amount, guess I’ll have to put it in with the formula)

for now i was just trying to read the formula string as a number or a letter, so thanks and i know i suck at name giving :wink:

Slow, not type safe, lacking in functionality compared to List.

You’d want to change your logic to handle that, then, but I guess you know that.

So you can easily create a new SetValues instance with the appropriate values, as shown on line 37 of my script.

–Eric

ok thanks again Eric think i start to get it.
problem is that it’s not really what i wanted.

my “logic” was/is i assign the formula in the inspector. than i read each char in the string and check first of all if the char is a letter or a number, depending on that i make a reading system that rewrites the chars in a manner i can use.
something like each char + is an atom name, the number i get behind each atom name string is the amount of atoms for that string. that way i can keep track or each atom used in the molecule and i can switch for molecule to the atoms used in the molecule and the other way around, i can make molecules with the right amount of atoms.
only thing i don’t what would be the best way is for the electron nr for each atom.
i have two main ideas for that, or i hard code it in somewhere… it’s not that much i only need the non metals electrons on the outer shell so between 1 or 8.
i could have a list of 8 with all the atom names (i think? never made something like this before but I’m sure it’s possible, so i can learn)
the second idea is just to adjust the formula syntax to include the electron nr so H2O could be something like 1H2,2O1, where 1 is the electron nr H the atom name and 2 the amount of atoms.

anyway if your still reading :wink: I’m having trouble using the list the way i used the array. i just learned here how to use a public class t o keep track, store or use my atom variables in a nice way, but this doesn’t work with a list i see, or at least i don’t get it to work. what would be the proper way to use the same kind of public class as that one with a list?

It does work. Actually you can’t have a public Array class in the inspector, but a public List works fine. In my code above, the atomList variable is shown in the inspector.

–Eric