my loop is not working properly

tsteven2

New member
Joined
May 3, 2022
Messages
4
Programming Experience
Beginner
bonjour a tous !

je suis débutant en programmation j'essaye de remplacer a partir d'un fichier les tags contenu dans un autre fichier.
le remplacement des tags s'effectue bien mais a chaque fois il repasse plusieurs fois sur la même ligne et je me retrouve avec un fichier de 100 lignes pourtant j'ai besoin de 20 lignes

merci de m'aider
C#:
 public List<string> Replacetag(string commandpath, Dictionary<string, string> dict)
        {
            List<string> replace = new List<string>();

            StreamReader sr = new StreamReader(commandpath);
            string Line;       

            while ((Line= sr.ReadLine()) != null)
            {

                foreach (KeyValuePair<string, string> uci in dict)
                {
                    if (Line!.Contains(uci.Key))
                    {
                        Line.Equals(Line);
                       

                    }
                    else if(Line.Contains(uci.Key))
                    {
                        Line = Line.Replace(uci.Key, uci.Value);
                        break;
                    }
                    replace.Add(Line);

                  
                   

                   for (int i = 0; i < replace.Count; i++)
                    {
                       if (replace.Contains(uci.Key))
                      {
                           replace.Remove(replace);
                      }
                    }

                }
            }
           
            
            return replace;         
        }


voici le resultat

opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
OVPN_DIR="/etc/openvpn"

et je veux

opkg update
opkg install openvpn-openssl openvpn-easy-rsa
 
Last edited by a moderator:
Bonjour! Welcome to the forum. Unfortunately this is an English language forum. Please re-post your question in English. Also, please put your code in code tags. The toolbar has an icon that looks like </> to make this easier.
 
I am a beginner in programming I try to replace from a file the tags contained in another file.
the replacement of the tags is done well but each time it goes back several times on the same line and I end up with a file of 100 lines yet I need 20 lines

please help me


C#:
public List<string> Replacetag(string commandpath, Dictionary<string, string> dict)
        {
            List<string> replace = new List<string>();

            StreamReader sr = new StreamReader(commandpath);
            string Line;       

            while ((Line= sr.ReadLine()) != null)
            {

                foreach (KeyValuePair<string, string> uci in dict)
                {               
                    if(Line.Contains(uci.Key))
                    {
                        Line = Line.Replace(uci.Key, uci.Value);
                        replace.Add(Line);
                        
                    }
                    else Line!.contains(uci.key)
                    {
                        Line.equals(Line)
                        replace.Add(Line)   
                    }
                    for (int i = 0; i < replace.Count; i++)
                    {
                        if (replace[i].Contains(uci.Key))
                        {
                            replace.Remove(replace[i]);
                        }
                    }

                }
            }
            foreach (string line in replace)
            {
               Console.WriteLine(line);
            }
                
            return replace;         
        }

THIS IS MYRESULT

opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg update
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
opkg install openvpn-openssl openvpn-easy-rsa
OVPN_DIR="/etc/openvpn"

I NEED THIS

opkg update
opkg install openvpn-openssl openvpn-easy-rsa
 
You could simply run the lines of the files through Distinct() LINQ extension method...
C#:
public List<string> GetUniqueLines(string fileName)
    => File.ReadLines(fileName).Distinct().ToList();
 
Last edited:
Does your code in post #3 even compile? Line 19 looks like a syntax error to me.

Looking at your code in post #1, I can see the reason for your duplicate lines. Line 24 will always add copy of the string read on line 8. Based on the output you presented, it looks like you 8 entries in your dictionary because the foreach loop spanning from lines 11-37.
 
Here's my recommended fix (in pseudocode):
C#:
List<string> ReadLinesWithTagsReplaced(string filename, Dictionary<string, string> replacements)
{
    return File.ReadLines(filename)
               .Select(line => ReplaceTags(line, replacements))
               .ToList();
}

string ReplaceTags(string line, Dictionary<string, string> replacements)
{
    if (!string.IsNullOrEmpty(line))
    {
        foreach(var pair in replacements)
        {
            line = line.Replace(pair.Key, pair.Value);
        }
    }
    return line;
}
 
Here's my recommended fix (in pseudocode):
C#:
List<string> ReadLinesWithTagsReplaced(string filename, Dictionary<string, string> replacements)
{
    return File.ReadLines(filename)
               .Select(line => ReplaceTags(line, replacements))
               .ToList();
}

string ReplaceTags(string line, Dictionary<string, string> replacements)
{
    if (!string.IsNullOrEmpty(line))
    {
        foreach(var pair in replacements)
        {
            line = line.Replace(pair.Key, pair.Value);
        }
    }
    return line;
}
its ready tankssssssssss
 

Latest posts

Back
Top Bottom