- Home /
Iterating through multidimensional arrays
I've been using http://wiki.unity3d.com/index.php?title=CSVReader to parse excel files that I've converted into CSV, the output that I end up with is a two-dimensional string array [y,x] such that with this input:
A,1,!
B,2,@
myArray[0,0] would be A, [0,1] would be 1, and [1,0] would be B. Once I have the 2d string grid, I want to go down the list vertically, and assign each row to a single-dimensional array textRow in some custom class:
class myData {
string[] textRow;
}
The problem is, how do I programmatically articulate the concept of a row within the 2d grid? I won't know how many columns there are in advance, so I can't hard code it, but I'm not sure how to iterate across multiple dimensions.
If you aren't sure of the number of rows, why don't use List ins$$anonymous$$d?
Answer by HarshadK · May 20, 2015 at 01:31 PM
Something like:
public TextAsset csvFile;
string[,] csvData; // This is your multidimensional array that stores the parsed CSV data.
string[] textRow;
void Start()
{
csvData = CSVReader.SplitCsvGrid(csvFile.text);
textRow = new string[csvData.GetUpperBound(0) + 1]; // Instantiate the textRow array with the number of rows in csvData array.
for(int i=0; i < textRow.Length; i++)
{
textRow[i] = csvData[i,0];
}
}
Answer by 0V3RR1D3 · May 20, 2015 at 05:39 PM
for(int x = 0 ; x < 100 ; x++){
for(int Y = 0 ; Y < 100 ; Y++){
//Acces the array like this
array[x,y] = "pugs";
}
}
I Hope that makes sence, Where x < 100 and y < 100, That number would be the x and y length of the array