Re: For Loop search problem
Moderator Action: Moved to C# Forum
Re: For Loop search problem
So, do you have the link of the location you have sent it?
Re: For Loop search problem
Quote:
Originally Posted by
nqioweryuadfge
So, do you have the link of the location you have sent it?
You're there... it's just an update to the forumid on the post... there wasn't a copy made, it was moved. so this is the only place the post is found.
As for your problem... Don't use a for loop. Use a different looping mechanism... when a replace is made, trip a flag. At the bottom of the loop, check the flag, if it's been set, then a replacement was made and you have to re-run the dictionary... if it wasn't set then increment i and go to the next.
But...
what happens if apple is replaced in Dictionary3.txt... does that mean it needs to go back to 1 and start all over again?
BTW - by restarting whether at the start of the current file, or all the way back at 1... you run the risk of an infinite loop it can't get out of...
-tg
Re: For Loop search problem
So, how can I prevent this implementation from jumping from Dictionary to Dictionary without allowing the first Dictionary to complete its searching? It was jumping from Dictionary 1 if it has AAA|BBB, to Dictionary 2 to replace BBB with CCC instead of allowing Dictionary 1 to complete its searches first. Apart from that, it does the same thing to other Dictionaries as well. I have made some changes, but don't know if this will solve the problem for good.
Code:
private string GetDictionaryData(string richText)
{
//richText = "You should accept change, and stop bad behavior. You must not do this things again.I tell the teacher.";
StringBuilder xml = new StringBuilder();
Dictionary<string, List<string>> replacements = new Dictionary<string, List<string>>();
for (int i = 0; i <= 7; i+1)
{
string fileName = "dictionary.txt";
if (i <= 0)
fileName = string.Format("dictionary{0}.txt", i);
using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/"+ fileName)))
{
while (!reader.EndOfStream)
{
string[] parts = reader.ReadLine().Split('|');
if (richText.Contains(parts[0]))
{
if (replacements.ContainsKey(parts[0]))
{
replacements[parts[0]].Add(parts[1]);
}
else
{
List<string> newWordList = new List<string>();
newWordList.Add(parts[1]);
replacements.Add(parts[0], newWordList);
}
}
}
}
}
Re: For Loop search problem
I just don't it to move to the next file without first allowing the current file to be checked completely. All the items in each Dictionary should be checked for all possible matches against the Text in the RichTextBox before moving to the next file.
Re: For Loop search problem
Have you tried stepping through the code to see what it is actually doing? there's nothing I can see that would stop it after a replace and move on to the next file. What I do see is that it would go through the first file, then move on to the next... again, nothing to do a restart though either.
I'll re-iterate, don't use a for loop.
to be honest tho... I'm not sure I follow this part of the code, which seems to be the meat of it:
Code:
string[] parts = reader.ReadLine().Split('|');
if (richText.Contains(parts[0]))
{
if (replacements.ContainsKey(parts[0]))
{
replacements[parts[0]].Add(parts[1]);
}
else
{
List<string> newWordList = new List<string>();
newWordList.Add(parts[1]);
replacements.Add(parts[0], newWordList);
}
}
you split the line... you check to see if the first element is int the rtb... and then add it to a list in a dictionary ... I don't see any replacement going on anywhere....
that's why I'm asking, have you stepped through the code to see what it is actually doing?
-tg
Re: For Loop search problem
ok, I see it will work. Thanks.
Re: For Loop search problem
I will post a parallel programming question I hope you answer.