Loading from CSV file into string Array

 

Hi, i'm in desperate need of help.

I have been trying to load string data from CSV into a string Array in different ways,

void LoadStringsFromCsv(string filename, string &stringArray[]) {
    int handle = FileOpen(filename, FILE_READ | FILE_CSV | FILE_ANSI);
    if (handle == INVALID_HANDLE) {
        Print("Error opening file: " + filename);
        return;
    }

    for (uint i = 0; i <= 100; i++) {
        string stringData = FileReadString(handle);
        //This did not work
        ArrayInsert(stringArray,i,stringData);
        //This did not work as well
        ArrayFill(stringArray,i,1,stringData);
    }
    FileClose(handle);
}

tried creating a local string arrayB and passing its only value to string arrayA (main array) with ArrayInsert(arrayA,arrayB,i,0,1) but it did not work either.

I used a very similar method with uint and it worked:

void CSVtoUint(string filename,uint &ArrayName[]){
  int handle=FileOpen(filename,FILE_CSV|FILE_READ);
  for (int i=0; i<=MRows; i++) {
    string dataStr = FileReadString(handle);
    ArrayName[i] = uint(dataStr); // Convert the string to an integer
    if (FileIsEnding(handle)) break;
  }
  FileClose(handle);
}


as well as matrixes:
matrix LoadCsvToMatrix(string filename, string DataDelimiter, matrix &Matrix) {
    int rowsTotal = 0;
    int handle = FileOpen(filename, FILE_READ | FILE_CSV | FILE_ANSI, DataDelimiter);
    if (handle == INVALID_HANDLE) {
       Print("Something went wrong loading Data...");
       return Matrix; }
    int rows = 0, columns = 0;
    while (!FileIsEnding(handle)) {
       string data = FileReadString(handle);
       if (rows == 0) {
          Matrix.Resize(rows + 1, columns + 1);
          Matrix[rows][columns] = data;
       }else {
           Matrix[rows][columns] = double(data);
       }
       columns++;
       if (FileIsLineEnding(handle)) {
          rows++;
          Matrix.Resize(rows, columns);
          columns = 0;
       }
    }
    rowsTotal = rows;
    FileClose(handle);
    Matrix.Resize(rowsTotal - 1, Matrix.Cols());
    return Matrix;
}

If you have any tips, suggestions or solutions, please reply.

 
gridmatix:

string stringData = FileReadString(handle);
        //This did not work
        ArrayInsert(stringArray,i,stringData);


Read carefully the doc about FileReadString() & ArrayInsert() :

FileReadString(): returns the line read (string).

ArrayInsert(): inserts the specified number of elements from a source array to a receiving one starting from a specified index.

 
gridmatix:

Hi, i'm in desperate need of help.

I have been trying to load string data from CSV into a string Array in different ways,

tried creating a local string arrayB and passing its only value to string arrayA (main array) with ArrayInsert(arrayA,arrayB,i,0,1) but it did not work either.


If you have any tips, suggestions or solutions, please reply.


I fixed the issue, i was just tired and could not imagine where i was wrong, mods feel free to delete if you want but i did not find any forum post about this topic so the result could be useful for someone in the future...

Here is the result:

//Load data from CSV file to string array
void LoadStringsFromCsv(string filename, string &stringArray[]) {
    int handle = FileOpen(filename, FILE_READ | FILE_CSV | FILE_ANSI);
    if (handle == INVALID_HANDLE) {
        Print("Error opening file: " + filename);
        return;
    }

    for (uint i = 0; i <= 100; i++) {
        string stringData = FileReadString(handle);
        stringArray[i] = stringData;
        if (FileIsEnding(handle)) break;
    }
    FileClose(handle);
}