1

I want a 2D array generated from a CSV file with unknown number of rows/columns. The column count is fixed based on the header data. I need to be able to process it as a grid going both across rows and down columns hence needing array.

At the moment, I can split the data into rows, then split each row into components. I then add each row to a list. This all seems to work fine.

What I cant do is convert a list of string arrays into a 2d array.

It currently is failing on the line string[,] newCSV = csvFile.ToArray(); with error Cannot implicitly convert type 'string[][]' to 'string[ * , * ]' so I'm obviously not declaring something properly - I've just no idea what!

List<string[]> csvFile = new List<string[]>();

void Start()
{
    // TODO: file picker
    TextAsset sourceData = Resources.Load<TextAsset>("CSVData");
    if (sourceData != null)
    { 
        // Each piece of data in a CSV has a newline at the end
        // Split the base data into an array each time the newline char is found
        string[] data = sourceData.text.Split(new char[] {'\n'} );

        for (int i = 0; i < data.Length; i ++)
        {
            string[] row = data[i].Split(new char[] {','} );
            Debug.Log(row[0] + " " + row[1]);
            csvFile.Add(row);
        }

        string[,] newCSV = csvFile.ToArray();

    } else {    
        Debug.Log("Can't open source file");
    }
2
  • 2
    List<string[]>.ToArray() results in a string[][], not a string[,]. Are you sure you declared your variable type properly? Commented Sep 18, 2019 at 16:51
  • 1
    There are two kinds of arrays in C# - n-Dimensional (e.g. 2D in your case) where the space allocated for each dimension is fixed, and jagged, which is an array of arrays, and where each row can contain a variable number of columns. You're mixing the two here. Commented Sep 18, 2019 at 17:23

2 Answers 2

3

Since your data is in the form of a table, I highly suggest using a DataTable instead of a 2d array like you're currently using to model/hold the data from your csv.

There's a ton of pre baked functionality that comes with this data structure that will make working with your data much easier.

If you take this route, you could then also use this which will copy CSV data into a DataTable using the structure of your CSV data to create the DataTable.

It's very easy to configure and use.

Just a small tip, you should always try to use data structures that best fit your task, whenever possible. Think of the data structures and algorithms you use as tools used to build a house, while you could certainly use a screw driver to pound in a nail, it's much easier and more efficient to use a hammer.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I didn't know this data type existed. I will read up more on it as I think it will be useful for future projects
Awesome, glad I could help!
2

You can use this function to get 2d array.

static public string[,] SplitCsvGrid(string csvText)
{
    string[] lines = csvText.Split("\n"[0]);

    // finds the max width of row
    int width = 0;
    for (int i = 0; i < lines.Length; i++)
    {
        string[] row = SplitCsvLine(lines[i]);
        width = Mathf.Max(width, row.Length);
    }

    // creates new 2D string grid to output to
    string[,] outputGrid = new string[width + 1, lines.Length + 1];
    for (int y = 0; y < lines.Length; y++)
    {
        string[] row = SplitCsvLine(lines[y]);
        for (int x = 0; x < row.Length; x++)
        {
            outputGrid[x, y] = row[x];

            // This line was to replace "" with " in my output. 
            // Include or edit it as you wish.
            outputGrid[x, y] = outputGrid[x, y].Replace("\"\"", "\"");
        }
    }

    return outputGrid;
}

1 Comment

I accepted this one as it basically fixes my code. However, the other answer about datatables is really useful as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.