.net - C# 4.0 - Multidimensional Associative Array (or way to mimic one?) -


i'm experienced php developer transitioning c#. @ present working on windows forms application.

i found in searches c# doesn't support associative arrays in same loose fashion php does. have found info on dictionary , "structs" seem class objects.

the trouble having getting head around not associative array, multi dimensional 1 want use keeping multiple counts in series of loops.

the application reading text log file, searching predefined string, pulling out date on line when string found, , incrementing count string match on date.

in php, easy this:

// initialize $count_array[$string_date][$string_keyword] = 0;  ...  // if string found $count_array[$string_date][$string_keyword] += 1;  ...  // ouput contents of array foreach($count_array $date -> $keyword_count_array) {     echo $date; // output date      foreach($keyword_count_array $keyword -> $count) {         echo $keyword . ": " . $count;     } } 

it seems little more involved in c# (which isn't bad thing). have tried using suggestion found on similar question don't follow how either increment or iterate/output contents:

// initialize var count_array = new dictionary<string, dictionary<string, int>>(); count_array = null;  ...  // if string found - think second reference supposed dictionary object?? count_array[string_date.toshortdatestring()][string_keyword]++;  ...  // ouput contents of "array" foreach (keyvaluepair<string, dictionary<string, int>> kvp in exportarray) {     foreach(keyvaluepair<string, int> kvp2 in kvp.value)      {         messagebox.show(kvp.key + " - " + kvp2.key + " = " + kvp2.value);     } } 

am on right track? or have better/cleaner method of mimicing php code above?

update

with above c# code, error @ "// if string found " line. error "object reference not set instance of object". assuming because have string in secound reference, not dictionary object. right now, unsure how increment.

update 2

thanks time. current code functional understanding how dictionary's work. advice regarding use of classes , objects situation not lost either. may refactor suit.

the code looks sound, thing see missing there no checks see if values exist before incrementing them.

before call

count_array[string_date.toshortdatestring()][string_keyword]++; 

you'll need do:

string shortdate = string_date.toshortdatestring(); if (!count_array.containskey(shortdate)) {     count_array.add(shortdate, new dictionary<string, int>()); }  if (!count_array[shortdate].containskey(string_keyword)) {     count_array[shortdate].add(string_keyword, 0); } 

before try incrementing anything.

you need initialize dictionary entries calling .add or ["key"] = value. calling ++ on uninitialized dictionary entry won't work. depending on you're trying accomplish though might idea use class.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -