Results 1 to 9 of 9

Thread: For Loop search problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Cool 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);
                                }
                            }
                        }
                    }
    
                }

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: For Loop search problem

    Moderator Action: Moved to C# Forum
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: For Loop search problem

    So, do you have the link of the location you have sent it?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: For Loop search problem

    Quote Originally Posted by nqioweryuadfge View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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);
                                }
                            }
                        }
                    }
                }

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: For Loop search problem

    ok, I see it will work. Thanks.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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
  •  



Click Here to Expand Forum to Full Width