deed02392
deed02392

Reputation: 5032

C# 2D array with string keys and int values

I'm trying to create a data structure that looks like the following:

             xls | doc | ppt | pdf
           |---------------------
app/xls    |  1  |  0  |  0  |  0  
app/msword |  1  |  1  |  0  |  0
app/ppt    |  0  |  0  |  1  |  0
app/pdf    |  0  |  0  |  0  |  1

Basically I have a List of Lists in which I have information like:

[ext, xls], [mime, app/xls]
[ext, ppt], [mime, app/ppt]
[ext, doc], [mime, app/msword]
[ext, xls], [mime, app/msword]

Note that sometimes the extension doesn't match up with the appropriate MIME value. So the reason for wanting the output like the table is to be able to see graphically what the distribution is of extensions not mapped to the correct MIME.

So I can loop through the List of Lists and access for each document both the ext and mime, but what I can't get my head around is how to extract/store hence output this information in tabular form. I have also a Dictionary of the correct ext-mime, which is important for ensuring that the diagonal line from the top-left of table to bottom-right, is the only place which should have non-zero values if the data is all correct (because then the order of the column-row matters in that case).

So where to go from here?!

Upvotes: 0

Views: 2334

Answers (1)

Sunny Gulati
Sunny Gulati

Reputation: 36

If I understand your question, you are looping through a bunch of data and populating the numbers (0's and 1's, but could be 2's.. N++) in the above structure.

Without getting fancy, I'd use

d = new Dictionary<string, Dictionary<string, int>>();   

If I wanted to get fancy, I'd create a class which had the following:

  • A List of the horizontal keys seen so far
  • A List of the vertical keys seen so far
  • A Dictionary of "key1#key2" with the value of 0|1|2...
  • A "setter" of (key1,key2) => add to horizontal keys and/or vertical keys if it doesn't exist
  • a "getter" of (key1,key2) => retrieves the value out of the dictionary

This will allow you to pull it out in a nice pretty grid with minimal effort, while maintaining decent write speed. It will also let you sort the rows / columns in whichever way you want. If you added a "OptimalMapping" to let it know what mimetype maps to what extension, you can very easily add a check to see if something was optimal or not.

Upvotes: 2

Related Questions