Returning values from a dictionary

Disclaimer: I’ve read at least twelve different dictionary explanations/answer/forum threads, the official dictionary document, and a tutorial video. These things are kicking my butt.

Would anyone have time to tell me in tiny words, maybe a crayon-drawn picture, how to capture the values stored in a single column (key?) and store them into a list or array?

My dictionary is below, borrowed from a (super nifty) Git CSV Reader. Even basic, manual entry, <string, string> dictionaries aren’t clicking for me, however.

List<Dictionary<string, object>> brokenBoundariesContent = CSVReader.Read("BrokenBoundariesContent");

What you have there, if the code works, is a list of dictionaries. Each dictionary uses a string for the lookup and has an object associated for each string.

What I am going to guess we have going on here is a CSV file with column names. I could be wrong, because I don’t have any access to your data or even the Git CSV Reader you are using.

So for example, imagine this CSV file. Ignore line numbers.

Room, Object, X, Y, Z
Library, Wrench, 3, 4, 5
Garden, Lead Pipe, 5, 2, 1
Drawing Room, Candlestick, 6, -1, 3

I am guessing likely get back a List of three dictionaries. The reader probably consumes the first line to understand what column names to use for the rest of the data below, and will use them as keys for the rest of the rows.

The first dictionary in the list likely has five keys and five objects. The keys would be “Room”, “Object”, “X”, “Y”, “Z” and if you request the object with the key “Room” you would get a string “Library”. If you request the object with the key “Y” you would get an int with a value of 4.

Dictionary<string, object> first = brokenBoundariesContent[0];
Debug.Log(first["Room"]); // prints Library in my example
first["Room"] = "Observatory" ; // replaces Library with Observatory

The second dictionary would match the same format. Just as the first row, the keys would be “Room”, “Object”, “X”, “Y”, “Z” and if you request the object with the key “Room” you would get a string “Garden”.

Now if you’ll excuse me, I have to talk about making wild speculative guesses with one Colonel Mustard.

2 Likes