First timer here. StackOverflow has helped me to somewhat grok arrays and lists, but I’m running into a problem that I don’t see answered elsewhere.
Goal: take an existing multidimensional array, read each line and if it starts with the correct identifier, pull that line into a list. Each list becomes part of my list of lists.
The problem: if I use newGrid[0].Add(CSVReader.grid[x,y]); below it runs fine. But if I use [y] instead of [0] I get an exception. Doesn’t y = 0 at the point I’m getting the exception? I would like to use the for loop’s y to make each line pulled from the array a new list.
public void processCSV () {
List<List<string>> newGrid = new List<List<string>>();
for (int y = 0; y < CSVReader.grid.GetUpperBound(1); y++) {
if (CSVReader.grid[0,y] == "T1") {
newGrid.Add(new List<string>());
for (int x = 0; x < CSVReader.grid.GetUpperBound(0); x++) {
newGrid[y].Add(CSVReader.grid[x,y]);
}
foreach(string item in newGrid[y]) {
print(item);
}
}
}
}
Line numbers in your
newGriddon’t match line numbers inCSVReader.gridbecause you only copy some lines from source array.Instead of reusing
yuse separate variable that counts lines in thenewGrideach time you callnewGrid.Addor simply add elements to last row.