开发者

combining hash tables and writing to file

i have 2 hash tables that i have created. Of those 2 hash tables, they both have the same key, but different values. I have sorted both of the hash tables using a sorted list to have the keys in order on both tables. What i am attempting to do is have both hash tables written to a text file, and in this text file, it will have the Key (which is the same in both hash tables) with the value from hash table one and the value from hash table 2 next to it. The data is separated by a TAB and will look something like what is below:

Key Value  Value
128   123      6
143   255      4
Hashtable frequency = new Hashtable();
Hashtable grouplist = new Hashtable();

SortedLis开发者_开发知识库t Grp = new SortedList (grouplist);
SortedList Freg = new SortedList(frequency);

foreach (DictionaryEntry entry in Grp)
{
    foreach (DictionaryEntry maxval in Freq)
    {
            file.Write(entry.Key);
            file.Write("\t");
            file.Write(entry.Value);
            file.Write("\t");

            if(Freq[entry.Key].Equals(Grp[maxval.Key]))
            {
                file.WriteLine(maxval.Value);
            }
     }
}

I have tried numerous ways to get it to write just the Key and Value from one hash table and the value from the second hash table, but it just repeats writing each Key 75 times (which is the total number of Keys both Hash Tables.

I have many variations of the above code and they all do the same thing (can post them if needed). Any help would be appreciated.

Also, the only way i can get it to work and write what is needed (75 times each) is if i put a - ! - in the if statement, even though both Keys are the same. I even checked to make sure they were the same when debugging the program. - Thanks


The problem is that you are looping 75 x 75 times. It is like your code is written like...

for (each of the 75 elements in one sorted lists)
    for (each of the 75 elements in the other sorted list)

What you want to do is only loop over one of your collections, using its Key to index into the other collection, like this...

foreach (DictionaryEntry entry in Grp)
    var Key = entry.Key;
    file.Write(Key);
    file.Write("\t");
    file.Write(frequency[Key].Value);
    file.Write("\t");
    file.Write(groupList[Key].Value);
    file.Write("\t");


Ideally you should just encapsulate your two hashtables into a single hashtable (then you don't have to worry about inconsistency, and the code to maintain the hashtables is easier). Barring that:

SortedList frequencies = new SortedList(frequency);
foreach(var entry in frequencies) {
    file.WriteLine(
        String.Format(
            "{0}\t{1}\t{2}",
            entry.Key,
            frequency[entry.Key],
            grouplist[entry.Key]
        )
    );
}


Have you tried something like this:

foreach (DictionaryEntry entry in Grp)
{
    file.Write(entry.Key);
    file.Write("\t");
    file.Write(entry.Value);

    if (Freq.ContainsKey(entry.Key))
    {
        file.Write("\t");
        file.Write(Freq[entry.Key]);
    }

    file.WriteLine();
 }

Basically, for every item in Grp, write it to the file. If Freq contains a matching key, write the value from Freq to the file. I also factored out your WriteLine call so that it always occurs (in your sample it does not).

I haven't tested this, but maybe it will give you something to go on.


I do not know, why you are using two foreach loops. I've changed your code as follows:

    Hashtable frequency = new Hashtable();
    Hashtable grouplist = new Hashtable();
    SortedList Grp = new SortedList(grouplist);
    foreach(DictionaryEntry entry in Grp) {
        file.Write(entry.Key);
        file.Write("\t");
        file.Write(entry.Value);
        file.Write("\t");
        object maxVal = grouplist[entry.Key];
        if(maxVal != null)
            file.WriteLine(maxVal);
        file.WriteLine();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜