2

I have two foreach loops, each of which loops through a text file, and gets the value of all of the values of the first two columns only (there are more than two columns in the text file, delimited by "|") and puts it in a string. I would like to compare the result of these foreach loops (the values that are output by the Response.Write statements) to see if the strings are equivalent or not. Any thoughts/suggestions appreciated.

protected void Page_Load(object sender, EventArgs e)
{
    string textFile1 = @"C:\Test\Test1.txt";
    string textFile2 = @"C:\Test\Test2.txt";
    string[] textFile1Lines = System.IO.File.ReadAllLines(textFile1);
    string[] textFile2Lines = System.IO.File.ReadAllLines(textFile2);
    char[] delimiterChars = { '|' };

    foreach (string line in textFile1Lines)
    {
        string[] words = line.Split(delimiterChars);
        string column1And2 = words[0] + words[1];
        Response.Write(column1And2);
    }

    foreach (string line in textFile2Lines)
    {
        string[] words = line.Split(delimiterChars);
        string column1And2 = words[0] + words[1];
        Response.Write(column1And2);
    }
}
Andy G
  • 19,232
  • 5
  • 47
  • 69
kyle_13
  • 1,173
  • 6
  • 25
  • 47
  • 1
    http://stackoverflow.com/questions/1955766/iterate-two-lists-or-arrays-with-one-foreach-statment-in-c-sharp – Gloria Aug 26 '13 at 19:15
  • Do you mean that you want to compare the first line of file 1 against the first line of file 2? Do you want to determine if every line matches, or compare the lines individually? – htxryan Aug 26 '13 at 19:15
  • Hi @RyanHenderson, doing the comparison line by line until there is an instance where it is not a match, then can stop comparing if such occurs. – kyle_13 Aug 26 '13 at 19:20

2 Answers2

2

One way to compare the outputs would be storing the strings as you go, and then compare the results using SequenceEqual. Since the two loops are identical, consider making a static method out of them:

// Make the extraction its own method
private static IEnumerable<string> ExtractFirstTwoColumns(string fileName) {
    return System.IO.File.ReadLines(fileName).Select(
         line => {
              string[] words = line.Split(delimiterChars);
              return words[0] + words[1];
         }
    );
}

protected void Page_Load(object sender, EventArgs e)
    // Use extraction to do both comparisons and to write
    var extracted1 = ExtractFirstTwoColumns(@"C:\Test\Test1.txt").ToList();
    var extracted2 = ExtractFirstTwoColumns(@"C:\Test\Test2.txt").ToList();
    // Write the content to the response
    foreach (var s in extracted1) {
        Response.Write(s);
    }
    foreach (var s in extracted2) {
        Response.Write(s);
    }
    // Do the comparison
    if (extracted1.SequenceEqual(extracted2)) {
        Console.Error.WriteLine("First two columns are different.");
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hi @dasblinkenlight, this solution worked, thanks so much. I'll have to brush up on the IEnumerable and SequenceEqual stuff. – kyle_13 Aug 26 '13 at 19:58
  • I like @dasblinkenlight's solution better than my own. I wasn't aware of SequenceEqual. Nice! – htxryan Aug 26 '13 at 20:20
1

I would simply compare in the same loop, using for instead of foreach:

protected void Page_Load(object sender, EventArgs e)
{
    string textFile1 = @"C:\Test\Test1.txt";
    string textFile2 = @"C:\Test\Test2.txt";
    string[] textFile1Lines = System.IO.File.ReadAllLines(textFile1);
    string[] textFile2Lines = System.IO.File.ReadAllLines(textFile2);
    char[] delimiterChars = { '|' };

    if (textFile1Lines.Count != textFile2Lines.Count)
    {
        // Do something since the line counts don't match
    }
    else
    {

    foreach (int i = 0; i < textFile1Lines.Count; i++)
    {
        string[] words1 = textFile1Lines[i].Split(delimiterChars);
        string compareValue1 = words1[0] + words1[1];

        string[] words2 = textFile2Lines[i].Split(delimiterChars);
        string compareValue2 = words2[0] + words2[1];

        if (!string.Equals(compareValue1, compareValue2))
        {
            // Do something
            break; // Exit the loop since you found a difference
        }
    }
}
}
htxryan
  • 2,871
  • 2
  • 21
  • 21