Text Import - C#



  • Im working on a program that was originally written in Access/VB that imports a text document to a database. Each text document has several thousand lines like so..
    "AN0","37","M","6/1/2008","F","A","S","N","Y","","",".058",".1188","41.66",60002,"False",1022

    I need to get a count of how many lines have the certain letters in the first string, so in this case i need 1 to A, N, and Zero. The rest are just matching the string to something then incrementing its value. Right now im using two arrays like so...

    char[] footnotecode = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '+', '0', '1', '2' };
    int[] footnotevalue = new int[footnotecode.Length];

    foreach (char c in contents[0])
    {
    if (footnotecode.Contains(c))
    {
    footnotevalue[Array.IndexOf(footnotecode, c)]++;
    }
    }

    For the rest of the items its just the If statement with no need for the foreach. While this is a huge improvement over the case statement monster that I replaced I still feels theres a better way to do it. If i havent made it clear enough let me know and ill provide any details.



  • Right sorry didnt know i had to use HTML tags in my post.

    Im working on a program that was originally written in Access/VB that imports a text document to a database. Each text document has several thousand lines like so..

    "AN0","37","M","6/1/2008","F","A","S","N","Y","","",".058",".1188","41.66",60002,"False",1022



    I need to get a count of how many lines have the certain letters in the first string, so in this case i need 1 to A, N, and Zero. The rest are just matching the string to something then incrementing its value. Right now im using two arrays like so...

    char[ footnotecode = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '+', '0', '1', '2' }; 
    int[ footnotevalue = new int[footnotecode.Length]; 
    
    foreach (char c in contents[0]) 
    { 
      if (footnotecode.Contains(c)) 
        { 
          footnotevalue[Array.IndexOf(footnotecode, c)]++; 
        } 
    }
    

    For the rest of the items its just the If statement with no need for the foreach. While this is a huge improvement over the case statement monster that I replaced I still feels theres a better way to do it. If i havent made it clear enough let me know and ill provide any details.


  • If you're using .Net 2.0 or later, than generics are your friend.  I'd do something like below.  You don't have to use a SortedList as there are other generic collections available like System.Collections.Generic.KeyValuePair<TKey,TValue) which will also work. 

                System.Collections.Generic.SortedList<char, int> summary = new System.Collections.Generic.SortedList<char, int>();
                char[] characters = new char[] { 'a', 'b', 'c' };

                //Initialize Summary
                foreach (char a in characters)
                {
                    if (!summary.ContainsKey(a))
                        summary.Add(a, 0);
                }

                foreach (char c in something)
                {
                    if (summary.ContainsKey(c))
                        summary[c]++;
                    else
                        System.Console.WriteLine("Key {0} not found.", c);
                }
     


Log in to reply