none
How can you compare 2 text files and only populate list key value pair with only the matching keys and values RRS feed

  • Question

  • ok i have 2 text files and they are diffent formats one is all on one line and one is in brakets examples below 

    file 1 << the one that is the template one i want to match the other 2 

    template

    the file i want to compare againt ( so check the file above for all text to the left side if there is a match in the file below add the key and value to list key value pair ) 

    file 2 

    File to compare

    so in the program i can add the value of the pair to template above im going to add to my program so as example 

    i know both file contain DisplayName blahblah so when it finds that those 2 files share the name be something like this 

    "displayName" "Nrg" 

    say i name the list key value pair 

    elfenlist so would want something like this 

    displayName = elfenlied[displayName];

    but not sure how to check files and get all occorences of the same names in both files

    Monday, January 27, 2020 11:54 PM

Answers

  • Hi elfenliedtopfan2,

    Thanks for your feedback.

    I make a little change to WayneAKing's code.

            static void Main(string[] args)
            {
                var comparee = File.ReadAllLines("compare.txt").ToList();
                Dictionary<string, string> dict = new Dictionary<string, string>();
    
                var templates = File.ReadAllLines("template.txt").ToList();
                for (int n = 0; n < templates.Count; ++n) // remove lines up to & including '{'
                {
                    if (!templates[n].Contains('{')) templates.RemoveAt(n--);
                    else
                    {
                        templates.RemoveAt(n);
                        break;
                    }
                }
                for (int x = 0; x < templates.Count; ++x) // remove tabs
                {
                    templates[x] = templates[x].Replace("\t", "");
                }
                List<string> lst = new List<string>();
                for (int y = 0; y < templates.Count; ++y)
                {
                    string[] sa = templates[y].Split(' ');
                    string str = sa[0].Trim('"');
                    if (comparee[0].Contains(str))
                    {
                        string result = str + " = file2match[" + str + "]";
                        lst.Add(result);
                    }
                }
                foreach (var ele in lst)
                {
                    Console.WriteLine(ele);
                }
                Console.ReadLine();
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 29, 2020 7:05 AM
    Moderator

All replies

  • Hi elfenliedtopfan2,

    Thank you for posting here.

    According to your description, I have a question to confirm with you.

    I find that 'gunModel' in file1 is

    "gunModel" "elfenliedtopfan5_nrg_vm"

    and in file2 is

    gunModel\t6_wpn_ar_ak47_view...

    Do you need something like this?

    gunModel = elfenliedtopfan5_nrg_vm[gunModel]

    Please provide more details about your question, and it will help us to make a test.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, January 28, 2020 5:34 AM
    Moderator
  • ok i have 2 text files and they are diffent formats one is all on one line and one is in brakets examples below 

    file 1 << the one that is the template one i want to match the other 2 

    the file i want to compare againt ( so check the file above for all text to the left side if there is a match in the file below add the key and value to list key value pair ) 

    file 2 

    so in the program i can add the value of the pair to template above im going to add to my program so as example 

    i know both file contain DisplayName blahblah so when it finds that those 2 files share the name be something like this 

    "displayName" "Nrg" 


    but not sure how to check files and get all occorences of the same names in both files

    It's not clear to me which file you want to save the values from, the template
    file or the file being compared. So here's some sample code that searches the
    file being compared for the name from each line of the template file. If it 
    finds that name in the file being compared (the one long string), then it
    prints the key/value pair from the *template* file to the console and saves
    the pair in a dictionary. At the end it prints all pairs from the dictionary.

    Some editing of text is done to remove tabs, etc. Assuming that you know how to
    program in C# you should be able to revise the code as needed to meet your
    gaming preferences. Keep in mind that we aren't here to provide a free program 
    writing service, so doing *all* of the coding for your requirements is not to
    be expected from us.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace CS_locate_matches_in_2_files_prob
    {
        class Program
        {
            static void Main(string[] args)
            {
                var comparee = File.ReadAllLines("comparee.txt").ToList();
                Dictionary<string, string> dict = new Dictionary<string, string>();
    
                var templates = File.ReadAllLines("templates.txt").ToList();
                for (int n = 0; n < templates.Count; ++n) // remove lines up to & including '{'
                {
                    if (!templates[n].Contains('{')) templates.RemoveAt(n--);
                    else
                    {
                        templates.RemoveAt(n);
                        break;
                    }
                }
                for (int x = 0; x < templates.Count; ++x) // remove tabs
                {
                    templates[x] = templates[x].Replace("\t", "");
                }
    
                for (int y = 0; y < templates.Count; ++y)
                {
                    var sa = templates[y].Split(' ');
                    if(comparee[0].Contains(sa[0].Trim('"')))
                    {
                        Console.WriteLine("{0} {1}", sa[0], sa[1]);
                        dict.Add(sa[0], sa[1]);
                    }                
                }
    
                Console.ReadLine();
                Console.WriteLine("\nDictionary:\n");
                foreach (KeyValuePair<string, string> kv in dict)
                {
                    Console.WriteLine("{0} {1}", kv.Key, kv.Value);
                }
                Console.ReadLine();
            }
        }
    }
    

    E&OE

    - Wayne

    Tuesday, January 28, 2020 5:41 AM
  • basicly file one will have a value to it as you can see for this example 

    "gunModel" "elfenliedtopfan5_nrg_vm"

    what i want to do ( is all going to be added to a writeline and wanted to do search both files find all matches if it has a match then 

    the second weapon file in key value pairs and then form there if there is gun model and it matches both files so file one gumodel.. is elfenliedtopfan5_nrg_vm and in the file 2 if there is a gunmodel entry in there 

    the template one file 1 i would want to replace the line elfenliedtopfan5_nrg_vm

    so in short 

    create a list with all file 2 matches to file one in this case i call it file2match

    gunmodel = file2match[gunmodel]

    so when the template is saved to file it will have all file2 matches included in template so it saves all the values of the keys to the template and then saves out.

    Tuesday, January 28, 2020 4:44 PM
  • Hi elfenliedtopfan2,

    Thanks for your feedback.

    I make a little change to WayneAKing's code.

            static void Main(string[] args)
            {
                var comparee = File.ReadAllLines("compare.txt").ToList();
                Dictionary<string, string> dict = new Dictionary<string, string>();
    
                var templates = File.ReadAllLines("template.txt").ToList();
                for (int n = 0; n < templates.Count; ++n) // remove lines up to & including '{'
                {
                    if (!templates[n].Contains('{')) templates.RemoveAt(n--);
                    else
                    {
                        templates.RemoveAt(n);
                        break;
                    }
                }
                for (int x = 0; x < templates.Count; ++x) // remove tabs
                {
                    templates[x] = templates[x].Replace("\t", "");
                }
                List<string> lst = new List<string>();
                for (int y = 0; y < templates.Count; ++y)
                {
                    string[] sa = templates[y].Split(' ');
                    string str = sa[0].Trim('"');
                    if (comparee[0].Contains(str))
                    {
                        string result = str + " = file2match[" + str + "]";
                        lst.Add(result);
                    }
                }
                foreach (var ele in lst)
                {
                    Console.WriteLine(ele);
                }
                Console.ReadLine();
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 29, 2020 7:05 AM
    Moderator
  • thank you this worked perfectly 
    Friday, January 31, 2020 7:06 PM