Editing Array of string values from CSV file

I'm new at this so please bear with me. I have a CSV file that contains about 500 rows and 50 columns. I need to keep all the rows but only keep columns 1,2,3,4,6,7. I've opened the CSV and stored each row as a single element in a one-dimensional array. What I need to do now is search through those array elements and remove everything except column 1,2,3,4,6,7. Am I approaching this problem correctly If so, how do I proceed I'm stuck.

Answer this question

Editing Array of string values from CSV file

  • ShadowOfTheBeast

    How can I read until the 7th column What would I need to add to the code I also need to omit the 5th column.
  • Tanmaya

    Thanks for the help. When I try to print the contents of the array, I get 500 values of System.Collections.Generic.List'1[System.String]. I tried a simple loop with Console::WriteLine(theArrayIdea) (the light bulb is supposed to be an i inside []) and then I stuck a line in where the values are being added to the array Console::WriteLine(lineValues) to see what was being added to the array. I do get a number of elements as 7 and line count of 500. Since this is a List, how do I print the array values


  • Janet666

    If you know the row size, why you are not stop reading at 7th column if the others are not relevant for you

    I'm not sure whether I'm answering for your question.
    I think you can do the following

    1. Read Single Line
    2. Read values till 7th Column
    3. Move to next line
    4. Go step 1 till end of rows



  • mark aoki

    Happy to help.

    I'm afraid I can't speak from experience regarding books on the managed extensions to C++ (known as C++/CLI). If I were you, I'd start my search on the following urls:

    Sticky faq in the C++ general forum, with some tips and links for books and resources:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=107484&SiteID=1

    Amazon search for C++/CLI, sorted by sales count. May have helpful reviews:

    http://www.amazon.com/s/ref=sr_st/104-2154958-2979945 keywords=C%2B%2B%2FCLI&page=1&rh=n%3A1000%2Ck%3AC%2B%2B%2FCLI&sort=salesrank&x=13&y=10

    MSDN has pretty good examples of the techniques used in the code I gave you. Look up some of the functions, and browse from there.

    Also, www.codeguru.com and www.codeproject.com are quite good sites, with tons of articles and tutorials. Definitely worth your time :)

    Good luck!



  • Zach7

    I adapted your sample code somewhat. This should get you further.

    using namespace System;
    using namespace System::IO;
    using namespace System::Collections;
    using namespace System::Collections::Generic;

    int main()
    {
        StreamReader^ reader = gcnew StreamReader("aaa.csv");
        List<List<String^>^ >^ theArray = gcnew List<List<String^>^ >(500);
     
        array<Char>^ delims = {','};
        while (0<reader->Peek())
        {
            String^ data = reader->ReadLine();
           
            // Split the comma separated line into a list of strings
            List<String^>^ lineValues = gcnew List<String^>((System::Collections::Generic::IEnumerable<String^>^)data->Split(delims));
           
            // Remove the 5th element
            lineValues->RemoveAt(4);
           
            // Remove all elements following the 7th
            lineValues->RemoveRange(7, lineValues->Count - 7);
           
            // Add values to the array
            theArray->Add(lineValues);
        }
       
        Console::WriteLine("Number of elements in the first row: {0}", theArray[0]->Count); // Should output 7
        Console::WriteLine("Line count: {0}", theArray->Count);

        return 0;
    }



  • Michael Grau

    Again, much thanks. You are a lifesaver.
  • bkizzy

    Thank you very much einaros. That's exactly what I need. Can you recommend a good book or site that contains information about this particular issue I'm new at this here at work and I need to get up to speed quickly.
  • C McQuade

    Try Console::WriteLine(theArray[row][column]) to print specific numbers, or Console::WriteLine(String::Join(",", theArray[row]->ToArray())) to print all the columns separated by commas.

  • Dave987654321

    Can you show us the code

     

    If you use a vector of vectors to store the elements of the file, you can do something very simple like this:

    std::vector<std::vector<int> > entries; // suppose 500 rows of 50 columns

    // now remove all the columns

    // iterate over all rows

    for(int i = 0; i<entries.size(); i++)

    {

      // keep only the first 7 columns

      entries[ i ].resize(7);
    }

    and that's all...



  • ZaaM IT

    Here's what I have so far. I'm using Visual Studio 2005. The Console::WriteLine statement and cout statements are only so I can see if it put the data in the array. It will be removed.

    #include <stdafx.h>

    #include <fstream>

    #include <iostream>

    #include <string>

    using namespace std;

    using namespace System;

    using namespace System::IO;

    using namespace System::Collections;

    int _tmain()

    {

    StreamReader^ reader;

    int lineCount = 0;

    reader = gcnew StreamReader("aaa.csv");

    String^ data;

    ArrayList^ theArray = gcnew ArrayList(500);

    while (0<reader->Peek())

    {

    data = reader->ReadLine();

    theArray->Add(data);

    Console::WriteLine(theArray[lineCount]);

    lineCount++;

    }

    cout << lineCount << endl;

    return 0;

    }


  • Editing Array of string values from CSV file