none
using a foreach to call a line, the previous line and the next line

    Question

  • Morning

    I have read a text file into a List<>, then I am calling a line that contains the word "Race", inside that if statement I am saying if the previous line contains 'data' AppendText(line); This works perfectly but I now want to call the next line after the word 'Race' and my attempts thus far have not succeeded. Here is my code so far, This is the IF statement inside my foreach:

    if (line.Contains("Race"))
                    {
                        if (racePreviousLine.Contains("Data"))
                        {
                                if (outputWindow.Text.Length > 0)
                                {
                                    outputWindow.AppendText(Environment.NewLine);
                                }
                            outputWindow.AppendText(racePreviousLine);
                            outputWindow.AppendText(line);
                        }
                    }
                    racePreviousLine = line;

    now I wish to say, if the line after 'Race' contains "PL" continue;

    I am doing this because this information is repeated throughout the text file and I only want the info where the line 'Race' is not followed by the line that contains 'PL' I did try to nest another IF statement basically saying what I just wrote the but I did not get the desired output. Can someone possibly give me an example where inside a foreach there are nested if statements calling a line that contains a word 'Race' then call the line before the line that contains the word 'Race', then call the line after the line that contains the word 'Race'.

    Thank You



    Thursday, March 30, 2017 9:59 AM

Answers

  • Various ways to approach this issue. One would be to use a for() loop
    instead of a foreach loop. The you can use relative indexing into the
    list. For example, if the current line is at list.ElementAt(idx)
    or list[idx] then the next line is obviously at idx + 1.

    If you insist on sticking with a foreach, you can still use indexing to
    reference a particular line in the list. But you will have to maintain
    an index yourself. For example, each iteration of the foreach you add
    one to an index variable. The current line will than be at the current
    index in the list.

    Simplified example (E&OE)

    List<String> lstr = new List<String>();
    
    lstr.Add("Line 1");
    lstr.Add("Line 2");
    lstr.Add("Line 3");
    
    int idx = 0;
    foreach(string s in lstr)
    {                
        if (s.Contains('2'))
        {
            Console.WriteLine(s);
            Console.WriteLine(lstr[idx + 1]);
            //Console.WriteLine(lstr.ElementAt(idx + 1)); // alternative        
        }
        idx++;
    }
    

    Output:

    Line 2                         
    Line 3                         

    Obviously you will have to do some bounds checking to ensure that you
    aren't trying to access a line that doesn't exist.

    - Wayne
    • Marked as answer by CuriousCoder79 Thursday, March 30, 2017 11:12 AM
    Thursday, March 30, 2017 10:50 AM

All replies

  • Various ways to approach this issue. One would be to use a for() loop
    instead of a foreach loop. The you can use relative indexing into the
    list. For example, if the current line is at list.ElementAt(idx)
    or list[idx] then the next line is obviously at idx + 1.

    If you insist on sticking with a foreach, you can still use indexing to
    reference a particular line in the list. But you will have to maintain
    an index yourself. For example, each iteration of the foreach you add
    one to an index variable. The current line will than be at the current
    index in the list.

    Simplified example (E&OE)

    List<String> lstr = new List<String>();
    
    lstr.Add("Line 1");
    lstr.Add("Line 2");
    lstr.Add("Line 3");
    
    int idx = 0;
    foreach(string s in lstr)
    {                
        if (s.Contains('2'))
        {
            Console.WriteLine(s);
            Console.WriteLine(lstr[idx + 1]);
            //Console.WriteLine(lstr.ElementAt(idx + 1)); // alternative        
        }
        idx++;
    }
    

    Output:

    Line 2                         
    Line 3                         

    Obviously you will have to do some bounds checking to ensure that you
    aren't trying to access a line that doesn't exist.

    - Wayne
    • Marked as answer by CuriousCoder79 Thursday, March 30, 2017 11:12 AM
    Thursday, March 30, 2017 10:50 AM
  • Thank you, Wayne

    Your suggestion about using a for loop is very good and, the for each example works well too.

    Thank 





    Thursday, March 30, 2017 11:24 AM