简体   繁体   中英

Iterating through a Dictionary of Dictionaries

Currently doing a bit of work on parsing a template I made for a small game.

It looks like this:

HEADER:NPC
NAME:Goblin
NAMEPLURAL:Goblins
NAMECHILD:Gob'in
NAMECHILDPLURAL:Gob'ins
MOVEMENT0:WALK
MOVEMENT1:RUN
MOVEMENT2:SWIM
BASEHP:30
BASEMANA:0
BASELEVEL:1?9

In my parser I do the following:

class TemplateParser
{
    public String folder { get; set; }

    public TemplateParser(String folder)
    {
        this.folder = folder;
    }

    public Dictionary<String, Dictionary<String, String>> Parse()
    {
        Dictionary<String, Dictionary<String, String>> parsedTemplates =
            new Dictionary<String, Dictionary<String, String>>();

        String[] files = Directory.GetFiles(folder);
        StreamReader reader;

        foreach (String s in files)
        {
            try
            {
                String line = "";
                reader = new StreamReader(s);
                Dictionary<String, String> attributes = new Dictionary<String, String>();
                while((line = reader.ReadLine()) != null)
                {
                    string[] split = line.Split(':');
                    attributes.Add(split[0], split[1]);
                }
                parsedTemplates.Add(Path.GetFileNameWithoutExtension(s), attributes);
                reader.Close();
            }
            catch (IOException ioe)
            {
                Log.LogMessage(ioe.Message, ELogFlag.CRITICAL);
            }
        }

        return parsedTemplates;
    }
}

Now here is the issue:

When I try to look through this collection (after running Parse() ) I want to do a little test and write out the dictionary containing all the info from the "Goblin" Dictionary.

So far all my attempts have been futile as when I do templates["Goblin"] I would expect I get a dictionary I could iterate over with foreach() but it wouldn't let me do that. It said something about the conversion not being possible. Then I look for an Enumerator instead but I couldn't get an enumerator to iterate over either.

What am I missing?

Without seeing your code for printing the dictionary, since the compiler "said something about the conversion not being possible," it appears that your code is not using the right element type in your loop.

Here is how you can iterate over a dictionary of dictionaries, regardless of their key types:

var dictOfDictionaries = Parse();
foreach (var dictPair in dictOfDictionaries) {
    Console.WriteLine("Key: {0}", dictPair.Key);
    foreach (var innerPair in dictPair.Value) {
        Console.WriteLine("\t{0}:{1}", innerPair.Key, innerPair.Value);
    }
}

Here is an example:

Dictionary<int,Dicationary<string,char>> lol = new Dictionary<int,Dicationary<string,char>>();
foreach( KeyValuePair<int, Dictionary<string,char>> kvp in lol)
{
    int test = kvp.Key;
    foreach(KeyValuePair<string,char> kvp2 in kvp.Value)
    {
        string s = kvp.Key;
        char c = kvp.Value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM