Really struggling with C# dictionaries

I usually work with PHP and I’m just unable to create complex arrays in C#.

I’m trying to make an item generator like in Diablo 2 and I’m parsing a csv-file into a dictionary.

Here’s my dictionary as it looks at the moment:

public static Dictionary<int, Dictionary<int, string[]>> prefixes = new Dictionary<int, Dictionary<int, string[]>>();The idea is that I’m grouping the rows in the csv-file into sub-dictionaries or what you’d call it based on a column in the file. I think it’s working fine, but I’m unable to see the contents of prefixes. My expected result is this:

Sorry, my tablet driver broke and my mouse drawing skills are awful.

In PHP I would just do print_r or whatever, but in C# I have no clue what do to.

I’m trying:

foreach (KeyValuePair<int, Dictionary<int,string[]>> prefixGroup in prefixes) {
    foreach (KeyValuePair<int, string[]> groupRow in prefixGroup) {
        foreach (string s in groupRow.Value) {
            Debug.Log(s);
        }
    }
}

But that just gives me this error:

Assets/Scripts/Items/FileReader.cs(48,9): error CS1579: foreach statement cannot operate on variables of type `System.Collections.Generic.KeyValuePair<int,System.Collections.Generic.Dictionary<int,string[]>>' because it does not contain a definition for `GetEnumerator' or is not accessible

It’s not hard to see what’s not working from the error, but how do I make it work? I’ve googled for hours.

Also, if there’s a better way of doing this then please let me know. :stuck_out_tongue:

prefixGroup in the second foreach, should be prefixGroup.Value

But if you are iterating over the entire collection, and using Ints as keys, would a List<List> not suffice? The only difference would be that the List<List> would need to be 0 based rather than 1 based in your drawing.

If it was a List<List> you could just for loop over it or use prefixes[prefixGroupIndex][groupRowInex] to get your string directly.

foreach (var l1 in prefixes)  // l1 = Dictionary<int,String[]>
     foreach (var l2 in l1.Value) // l2 = string[]
          foreach (var l3 in l2.Value) // l3 = string
          { };

Using an IDE makes it easy :slight_smile:

You are trying to use prefixGroup as a Dictionary, but the Dictionary is it’s value (it’s Key is the int).

foreach (KeyValuePair<int, Dictionary<int, string[]>> prefixGroup in prefixes){
	// prefixGroup is a KVP - not a Dictionary<int, string[]>
	// so you need to use it's .Value to get to the next level
	foreach(KeyValuePair<int, string[]> groupRow in [B]__prefixGroup.Value__[/B]){
		foreach(string s in groupRow.Value){
			Debug.Log(s);
		}
	}
}

// You could make it shorter by saying
foreach(Dictionary<int,string[]> groupRow in prefixes.Values){
	foreach(string s in groupRow.Values){
		Debug.Log(s);
	}
}

Great, that did the trick.

@Ntero, I do need the outer Dictionary because group can be any number, but I didn’t need the inner dictionary so I replaced that with a List.

@NPSF3000, I’m using Monodevelop, but it didn’t help me enough apparently. :slight_smile:

Thanks a lot guys.