By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

using hashtables

P: n/a
I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?

*** Sent via Developersdex http://www.developersdex.com ***
Jan 3 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 03 Jan 2008 12:00:21 -0800, Mike P <mi*******@gmail.comwrote:
[...]
Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?
While the point of having your data duplicated in two different places is
lost on me, the answer to your specific question is:

You can use foreach() to enumerate KeyValue<pairs in the original hash
table, checking for the presence of the Key of the KeyValue<instance in
the second hash table, and use the Add() method in the usual way to add
the KeyValue<pair to the third table if it's not present in the second.

Pete
Jan 3 '08 #2

P: n/a
Mike P wrote:
I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?
It's pretty straightforward (pseudocode):

foreach (Item in Hashtable 1)
{
if (Hasthtable 2.Contains(Item))
continue;
else
Hashtable 3.Add(Item);
}

What specifically are you having difficulty with?
Chris.
Jan 3 '08 #3

P: n/a
Hi Mike,

It has been a while since I last saw one post from you, I hope you had a
nice holidays.

You would have to iterate in the first hash and check the existence of the
key in hash2, if not present insert into hash 3

It should be plain simple to implement.
"Mike P" <mi*******@gmail.comwrote in message
news:Ot**************@TK2MSFTNGP03.phx.gbl...
>I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Jan 3 '08 #4

P: n/a
give us more detail if you want an optimized solution.

how the second hash is generated.

why you have a second hashtable with a subset of data from fist hashtable,
you can simple mark items from second hashtable in the first hashtable

"Mike P" wrote:
I have 2 hashtables each of which I am using to store a set of IDs and
Descriptions. Hashtable 1 will have the full set of data (e.g. IDs
1,2,3,4,5) whereas Hashtable 2 will only have a subset of that data
(e.g. IDs 2,4).

What I want to do is to iterate through Hashtable 1 (using foreach?)
checking if each ID/Description is in Hashtable 2, and if not, then I
want to add the ID/Description into Hashtable 3. So by the end of this
I will have :

Hashtable 1 : complete set of data
Hashtable 2 : subset of data
Hashtable 3 : data in Hashtable 1 but not in Hashtable 2

Can anybody tell me how to iterate through my hashtables, and how to add
the data (IDs and Descriptions) to the third hashtable?

*** Sent via Developersdex http://www.developersdex.com ***
Jan 3 '08 #5

P: n/a
This is the bit I am having problems with :

Here is the syntax for populating my first two Hashtable :

hst1.Add(objDataReader["ApplicationID"],
objDataReader["ApplicationName"]);

Then I need to use a foreach loop to check for matches between the 2
hashtables and if there isn't a match, then add the data to a third
hashtable. But I am not sure about the syntax required :

foreach (int AppID in hst1)
{
if (hst2.Contains(AppID))
{
continue;
}
else
{
hst3.Add(hst1(AppID, AppName));
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '08 #6

P: n/a
On 4 Jan, 09:19, Mike P <mike.p...@gmail.comwrote:
This is the bit I am having problems with :

Here is the syntax for populating my first two Hashtable :

hst1.Add(objDataReader["ApplicationID"],
objDataReader["ApplicationName"]);

Then I need to use a foreach loop to check for matches between the 2
hashtables and if there isn't a match, then add the data to a third
hashtable. *But I am not sure about the syntax required :

foreach (int AppID in hst1)
* * * * * * * * {
* * * * * * * * * * if (hst2.Contains(AppID))
* * * * * * * * * * {
* * * * * * * * * * * * continue;
* * * * * * * * * * }
* * * * * * * * * * else
* * * * * * * * * * {
* * * * * * * * * * * * hst3.Add(hst1(AppID, AppName));
* * * * * * * * * * }
* * * * * * * * }

*** Sent via Developersdexhttp://www.developersdex.com***
System.Collections.Hashtable h1 = new Hashtable();
System.Collections.Hashtable h2 = new Hashtable();
System.Collections.Hashtable h3 = new Hashtable();

h1.Add("Key1","value1");
h1.Add("Key2","value2");
h1.Add("Key3","value3");
h2.Add("Key2","value2");

foreach(string key in h1.Keys)
{
if (!h2.Contains(key))
{
h3.Add(key, (string)h1[key]);
}
}

note, I'm just using strings for everything to keep it simple.
Jan 4 '08 #7

P: n/a
Thanks...there's one more question I have on hashtables...how do I then
add the contents of a hashtable to a listbox for example?
*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.