none
how would I parse through this comma delimited text file to create two List<Integer> s ?

    Question

  • How best would I solve the following given this great template I've been given by Jon Galley.
    I don't want to destroy the elegant solution he and others have helped me create.
    This is the final challenge that will parse a text file into Lists<> which will be overlayed inputs into a GigaSoft graph.
    Thank you!

    // The two Arrays below are both within logBlocks[5].
    // I would like to utilize extractLines and extractFields
    // to create 2 List<Integers> for Array 1 and Array 33 below.
    // "Array 1 start"  and  "Array 1 end" and
    // "Array 33 start" and  "Array 33 end" defines boundaries.
    
    String[] logFields ;
    String blockDelimiter = "#*****************************************";
    String lineDelimiter = Environment.NewLine;
    String fieldDelimiter = ",";
    
    String logData ;
    String[] logBlocks ;
    String[] logLinesCellValuesForLinearGraph;
    
    logBlocks = extractBlocks(logData, blockDelimiter);
    
    // The next line of code results in the Array1 and Array33 value below.
    logLinesCellValuesForLinearGraph = extractLines(logBlocks[5], lineDelimiter);
    
    // The next line of code will be needed to help with the extraction of the comma
    // delimited values in Array 1 and Array 33.
    logFields = extractFields(logLinesCellValuesForLinearGraph[1], fieldDelimiter);
    
    //Array 1 start
    //58,59,59,59,59,59,59,59,59,59,59,58,58,57,57,57,56,57,56,57,
    //57,57,57,56,57,56,56,57,57,58,58,57,57,57,56,56,55,56,56,55,
    //Array 1 end
    //Array 33 start
    //90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,
    //90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,
    //Array 33 end
    
    private string[] extractBlocks(string p, string delimiter)
    {
        return p.Split(new String[] { delimiter }, StringSplitOptions.None);
    }
    
    private string[] extractLines(string p, string delimiter)
    {
        return p.Split(new String[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
    }
    
    private string[] extractFields(string p, string delimiter)
    {
        String[] f = p.Split(new String[] { delimiter }, StringSplitOptions.None);
    
        for (int i = 0; i < f.Length; i++)
        {
            f[i] = f[i].Trim();
        }
        return f;
    }
    • Edited by hazz Thursday, December 17, 2009 6:34 PM
    Thursday, December 17, 2009 6:22 PM

Answers

  • Thank you Balaji.
    What I list below works for my application and I thank each of you for your help.
    I know I could have writtent this code "snippet" differently but I think it won't
    look too out of place within the code design so many have contributed to here.
    Now I get to add 3 arrays to a graph overlay.
    This finishes a task between localization and TCP Socket messaging.
    How different than the Silverlight/WCF way of doing business I spent the first 9 months of 2009 working on.
    Now you know how I accomplish all this, with my forum community support !
               List<int> fieldsArray1  = new List<int>();
               List<int> fieldsArray33 = new List<int>();
    
               for (int i = 1; i < logLinesCellValuesForLinearGraph.Length; i++)
               {
                   logFields = extractFields(logLinesCellValuesForLinearGraph[i], fieldDelimiter);
                   foreach (string str in logFields)
                   {
                       if (str != string.Empty)
                       {
                           switch (str)
                           {
                               case "Array 1 start":
                                   bBeginParseArray1 = true;
                                   break;
                               case "Array 1 end":
                                   bBeginParseArray1 = false;
                                   break;
                               case "Array 33 start":
                                   bBeginParseArray33 = true;
                                   break;
                               case "Array 33 end":
                                   bBeginParseArray33 = false;
                                   break;
                               default:
                                   if (bBeginParseArray1) fieldsArray1.Add(Convert.ToInt32(str));
                                   if (bBeginParseArray33) fieldsArray33.Add(Convert.ToInt32(str));
                                   break;
                           }
                       }      //if (str != string.Empty)
    
                   }          //foreach (string str in logFields)
               }              // for (int i = 1; i < logLinesCellValuesForLinearGraph.Length; i++)


    • Marked as answer by hazz Thursday, December 17, 2009 8:05 PM
    Thursday, December 17, 2009 8:05 PM

All replies

  • I want to help, but I'd like to see you give it a try and then post code where your parsing is failing.  I've seen numerous posts from your recently and it almost seems like you are having forum members right your code for you.
    Thursday, December 17, 2009 6:28 PM
  • Your point is very well taken.
    I just edited my original post here for my own clarification and will try to solve this myself.
    Not to avoid your point, my need to clarify my questions has helped me alot.
    I have been surprised at how quickly responders have solved my problem before I've had a chance to work through it myself !!!
    I'll admit many of the responses helped me with ideas I never would have solved on my own.
    I've also been asking syntactical questions I should know the answers to.

    Some ideas I will still be reviewing after my solution is sufficient, like using the Dictionary rather than List of Lists.

    I promise that this is my final post/thread for this particular file processing task.
    I sincerely have not intended to take advantage of so much talent without putting out a signficant amount of thought and effort myself.

    Time for me to get back to work and see if I can be the one to post the solution which is what I always hope I can do !

    Thursday, December 17, 2009 6:47 PM
  • ...the "logFields" variable will store the string array by calling the function "extractFields()"; declare a variable for your integer list and create a foreach loop to get all the string variables from the string array and convert them to int and add it to the list.

                // The next line of code will be needed to help with the extraction of the comma
                // delimited values in Array 1 and Array 33.
                logFields = extractFields(logLinesCellValuesForLinearGraph[1], fieldDelimiter);
    
                List<int> fields = new List<int>();
    
                foreach (string str in logFields)
                {
                    fields.Add(Convert.ToInt32(str));
                }
    

    Balaji Baskar [Please mark the post as answer if it answers your question]
    Thursday, December 17, 2009 7:24 PM
  • Your point is very well taken.
    I just edited my original post here for my own clarification and will try to solve this myself.
    Not to avoid your point, my need to clarify my questions has helped me alot.
    I have been surprised at how quickly responders have solved my problem before I've had a chance to work through it myself !!!
    I'll admit many of the responses helped me with ideas I never would have solved on my own.
    I've also been asking syntactical questions I should know the answers to.

    Some ideas I will still be reviewing after my solution is sufficient, like using the Dictionary rather than List of Lists.

    I promise that this is my final post/thread for this particular file processing task.
    I sincerely have not intended to take advantage of so much talent without putting out a signficant amount of thought and effort myself.

    Time for me to get back to work and see if I can be the one to post the solution which is what I always hope I can do !


    Personally, I think one or two of your questions could have been answered without our assistance.  Overall, you consistently mark all the answers that are helpful and we as answerers receive the points and stuff so its cool with me to ask alot of questions.  Just be sure they are well thought out so you don't appear to be lacking in effort.
    (//.T) Former member of Emo
    Thursday, December 17, 2009 7:43 PM
  • Thank you Balaji.
    What I list below works for my application and I thank each of you for your help.
    I know I could have writtent this code "snippet" differently but I think it won't
    look too out of place within the code design so many have contributed to here.
    Now I get to add 3 arrays to a graph overlay.
    This finishes a task between localization and TCP Socket messaging.
    How different than the Silverlight/WCF way of doing business I spent the first 9 months of 2009 working on.
    Now you know how I accomplish all this, with my forum community support !
               List<int> fieldsArray1  = new List<int>();
               List<int> fieldsArray33 = new List<int>();
    
               for (int i = 1; i < logLinesCellValuesForLinearGraph.Length; i++)
               {
                   logFields = extractFields(logLinesCellValuesForLinearGraph[i], fieldDelimiter);
                   foreach (string str in logFields)
                   {
                       if (str != string.Empty)
                       {
                           switch (str)
                           {
                               case "Array 1 start":
                                   bBeginParseArray1 = true;
                                   break;
                               case "Array 1 end":
                                   bBeginParseArray1 = false;
                                   break;
                               case "Array 33 start":
                                   bBeginParseArray33 = true;
                                   break;
                               case "Array 33 end":
                                   bBeginParseArray33 = false;
                                   break;
                               default:
                                   if (bBeginParseArray1) fieldsArray1.Add(Convert.ToInt32(str));
                                   if (bBeginParseArray33) fieldsArray33.Add(Convert.ToInt32(str));
                                   break;
                           }
                       }      //if (str != string.Empty)
    
                   }          //foreach (string str in logFields)
               }              // for (int i = 1; i < logLinesCellValuesForLinearGraph.Length; i++)


    • Marked as answer by hazz Thursday, December 17, 2009 8:05 PM
    Thursday, December 17, 2009 8:05 PM
  • As long as you are learning something then I am happy...I just want to make sure your putting in some effort to or else you'll never get better. 
    Thursday, December 17, 2009 9:53 PM