Results 1 to 6 of 6

Thread: 2 column csv file to 2 dim array

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    2 column csv file to 2 dim array

    Im new to C# and was wondering if there was a more elegant way to do the following:
    thanks in advance for any help!
    VB Code:
    1. if (ofd1.ShowDialog() == DialogResult.OK)
    2.             {
    3.                 System.IO.StreamReader objFile = new System.IO.StreamReader(ofd1.FileName);
    4.                 string[] arrFirst = objFile.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    5.                
    6.                 objFile.Close();
    7.                 objFile.Dispose();
    8.                 //int intUB = arrFirst.GetUpperBound(0);
    9.                 string[,] arrSecond;
    10.                 arrSecond = new string[ arrFirst.GetUpperBound(0),2];
    11.                 string[] arrTemp;
    12.                 arrTemp = new string[1];
    13.  
    14.                 for (int intCounter = 0; intCounter <= arrFirst.GetUpperBound(0) - 1; intCounter++)
    15.                 {
    16.                     arrTemp = arrFirst[intCounter].Split(',');
    17.                     arrSecond[ intCounter,0] = arrTemp[0];
    18.                     arrSecond[intCounter,1] = arrTemp[1];
    19.                 }
    20.  
    21.  
    22.             }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 column csv file to 2 dim array

    As I mentioned in another thread you posted to, your use of Split is incorrect. The overload you're using will split on evry occurrence of any character in the array you specify, which means that it will split on every '\r' and every '\n' and add an empty entry for the zero length string between each pair. The RemoveEmptyEntries will then remove them so your end result is the same, but why put them in only to remove them? You want to split on every line break, and a line break is the "\r\n" substring, so that's what you need to specify:
    Code:
    string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
    That will split on every occurrence of any substring in the array, and "\r\n" is the only one, so it will split on the line breaks.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: 2 column csv file to 2 dim array

    Quote Originally Posted by jmcilhinney
    As I mentioned in another thread you posted to, your use of Split is incorrect. The overload you're using will split on evry occurrence of any character in the array you specify, which means that it will split on every '\r' and every '\n' and add an empty entry for the zero length string between each pair. The RemoveEmptyEntries will then remove them so your end result is the same, but why put them in only to remove them? You want to split on every line break, and a line break is the "\r\n" substring, so that's what you need to specify:
    Code:
    string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
    That will split on every occurrence of any substring in the array, and "\r\n" is the only one, so it will split on the line breaks.
    not sure if you saw my other reply, but the code works fine for me as i posted it. I cant seem to get yours to work though. Please help me see what I am missing.

    The whole thing the way i wrote it does seem kinda clunky to me, though.

    and thanks for the help!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 column csv file to 2 dim array

    As I said, your code will produce the correct end result but it goes about getting that result in an unnecessarily roundabout way. Test this code:
    Code:
    string str = "Line 1\r\nLine 2\r\nLine 3";
    
    string[] splitWithCharArray = str.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None);
    string[] splitWithStringArray = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    
    MessageBox.Show(string.Join(Environment.NewLine, splitWithCharArray));
    MessageBox.Show(string.Join(Environment.NewLine, splitWithStringArray));
    and you'll see how using ToCharArray creates more elements than are required. That's because your method splits the original string on every '\r' and also on every '\n', while my method splits it only on every "\r\n", so your method splits at twice as many points. By specifying RemoveEmptyEntries you are telling Split to remove those additional empty entries, but they are still created to begin with. As I said, why put them in only to remove them when you don't have to put them in in the first place?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: 2 column csv file to 2 dim array

    that does work! I was really more worried about the other areas of the snippet though. I just kinda feel like im going the long way about getting the data into a 2 dim array.

    Thanks for the help!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2 column csv file to 2 dim array

    You're going to have to use a loop somewhere along the line. Assuming that your file is known to conform to the correct format, this is about as good as you're going to get:
    Code:
    string[] lines = System.IO.File.ReadAllLines(ofd.FileName);
    string[,] matrix = new string[lines.Length, 2];
    string[] lineParts;
    
    for (int i = 0; i < lines.Length; i++)
    {
        lineParts = lines[i].Split(',');
        matrix[i, 0] = lineParts[0];
        matrix[i, 1] = lineParts[1];
    }
    If you don't know for sure that the file conforms to the correct format then I'd advise this:
    Code:
    string[] lines = System.IO.File.ReadAllText(ofd.FileName).Split(new string[] { Environment.NewLine },
                                                                    StringSplitOptions.RemoveEmptyEntries);
    string[,] matrix = new string[lines.Length, 2];
    string[] lineParts;
    
    for (int i = 0; i < lines.Length; i++)
    {
        lineParts = lines[i].Split(',');
        matrix[i, 0] = lineParts[0].Trim();
    
        if (lineParts.Length > 1)
        {
            matrix[i, 1] = lineParts[1].Trim();
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width