Reputation: 1233
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);
}
}
Upvotes: 2
Views: 1243
Reputation: 2961
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
}
}
}
}
Upvotes: 1
Reputation: 726479
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.");
}
}
Upvotes: 2