|
-
Jan 19th, 2017, 07:36 AM
#1
Thread Starter
Lively Member
For Loop search problem
I am trying to avoid a for loop, I already have that code you have written, but I want to avoid this:
i ++
I want to check each dictionary procedurarly. It is jumping from Dictionary to Dictionary looking for matches. So, I want it to have conditions that no Dictionary can be searched until all possible matches in Dictionary 1 are found before moving to Dictionary 2.
But the problem with this code is that, if it encounters a word replaced in Dictionary 1 .E.g. I have replaced Bulk|Apple from Dictionary 1. If apple is what I have chosen. And now Apple is being replaced by another word in Dictionary 2. E.g. Apple|Passion. The code will not allow Dictionary 1 to finish scanning the whole document. Instead, it will jump to Dictionary 2 to solve that problem then jump to wherever it is it will jump next. This is a very bad code to implement. Can you help?
Code:
for (int i = 0; i < 8; i++)
{
string fileName = "dictionary.txt";
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);
}
}
}
}
}
-
Jan 19th, 2017, 09:44 AM
#2
Re: For Loop search problem
Moderator Action: Moved to C# Forum
-
Jan 19th, 2017, 11:15 AM
#3
Thread Starter
Lively Member
Re: For Loop search problem
So, do you have the link of the location you have sent it?
-
Jan 19th, 2017, 11:38 AM
#4
Re: For Loop search problem
 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
-
Jan 22nd, 2017, 02:42 AM
#5
Thread Starter
Lively Member
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);
}
}
}
}
}
-
Jan 24th, 2017, 11:26 AM
#6
Thread Starter
Lively Member
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.
-
Jan 24th, 2017, 11:52 AM
#7
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
-
Jan 25th, 2017, 02:31 AM
#8
Thread Starter
Lively Member
Re: For Loop search problem
ok, I see it will work. Thanks.
-
Jan 25th, 2017, 11:53 AM
#9
Thread Starter
Lively Member
Re: For Loop search problem
I will post a parallel programming question I hope you answer.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|