2D array problem in C#

Hi,

In my external file, I have this unique and short line:

001///526.9785///918.133///001///1484.1///397.38///

You can read it like this:

type1///x coordinate of type0///y coordinate of type0///type2///x coordinate of type1///y coordinate of type1

I successfully get Unity to read the file and return this a single string variable; this is not a problem in any way.
However, I must split this retrieved string to smaller chunks so that I can store the string values seperately in variables, just like this:

string xoffirstobject = x coordinate of type0;

string yoffirstobject = y coordinate of type0;

string xofsecondobject = x coordinate of type1;

string yofsecondobject = y coordinate of type1;

I am new to multi-dimensional arrays, and all answers that could help me understand are mainly coded in Javascript or too specific to somebody else’s situation, and I am coding in C# only. I tried to understand the Javascript examples (did many tests to adapt their code) for 7 hours now but all I tried to find is a huge headache…

How to proceed? Here below I post my code. Don’t take much attention to my actual syntax, I know it is not logic or returning a 2D array, but instead please tell me how to achieve this in the actual context.

Here it is (sorry I do not include variable declarations):

		if(GUI.Button(new Rect(90,10,70,30), "Load...")) 
		{
            //Retrieve the text from the external text file and store it in a string variable
			theSourceFile = new FileInfo ("C:/unityexport/test.txt");
        	reader = theSourceFile.OpenText();
			text = reader.ReadLine();
			reader.Close();

            //I set u<3 because I have only 2 types currently in my external text file. But this is the part that is not working properly.
            for (int u=1;u<3;u++)
            {
			string[] dataLinestype = text.Split("///");
		string[] dataLinesx = text.Split("///"[0]);	
		string[] dataLinesy = text.Split("///"[0]);
			
		   Debug.Log("............................ type: " + dataLinestype + " x: " + dataLinesx[0] + " y: " + dataLinesy[0]);

}

	}

NOTE that I am generating the text file myself, so I can put any seperating character at any desired position if this is a problem. I want the most simple-to-understand way.
Please help me!

The method Split splits a string into an Array on the basis of the given delimiter characters.

With that you don’t need the first loop for (int u=1;u<3;u++) because you can split the whole content of your file.
The second issue is that the parameter you are passing to the split method is “///” or “///”[0]. Each string is nothing but a character array - here you take the array [‘/’, ‘/’, ‘/’] and get the item at “u” or 0.
What you might have thought of is this line text.Split("///".ToCharArray(), u) or similar but this would take each ‘/’ as delimiter instead of the whole sequence “///”.
I would recommend using two delimiters to reflect the structure of a 2D-Array and using regular expressions if you need to take a character sequence as delimiter.
private List<string> data = new List<string>();

private Regex itemRegex = new Regex(“—”);
private Regex valueRegex = new Regex(“///”);

// Use this for initialization
void Start ()
{
//coming from extern
string content = “001///526.9785///918.133—001///1484.1///397.38”;

//the items were split by "---" and stored in dataRoot
string[] dataRoot = itemRegex.Split(content);

foreach (string item in dataRoot) 
{
	//each item were split here by "///". 
	//The result is an array with all values of that item.
	string[] itemValues = valueRegex.Split(item);
	
	//all values representing the item are added to a list. There could be 2 items or thousands. 
	data.Add(itemValues);
	
	//Debug:
	Debug.Log(string.Format("Item root ({0})", item));
	foreach (string v in itemValues) 
	{
		Debug.Log(string.Format("		- value: {0}", v));
    }
	Debug.Log("

");

}
}
Don’t forget to use ‘System.Text.RegularExpressions’ for RegExp and ‘System.Collections.Generic’ for the List.
If you want it even easier you could create a Json-string and parse it right into your model class but thats another part.

You can write your triplet like this :
001,526.9785,918.133;001,1484.1,397.38;

and split them with :

string[] triplets = data.Split(';');
for (int i = 0; i < triplets.Length; ++i)
{
  string[] values = triplets*.Split(',');*

if (values.Lenght != 3)
continue;

int type = int.Parse(values[0]);
float xValue = float.Parse(values[1]);
float yValue = float.Parse(values[2]);

// Do what you want with values…
}
Hope it help :slight_smile: