473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Datastructure

Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?

Thank you in advance.
Shahid

Apr 13 '06 #1
3 1242

Shahid wrote:
Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?


Just create a class that encapsulates key1 and key2, and use objects of
that class as the keys.

(This assumes you will always have both key1 and key2 available - if
you want to be able to do something like 'give me all values that have
key1=X and key2=anything', then more work would be needed)

--
Larry Lard
Replies to group please

Apr 13 '06 #2
On 13 Apr 2006 07:13:17 -0700, "Shahid" <sh*********@gmail.com> wrote:
Hello,

I am trying to store data from the database and then process it later
in the application.

I was wondering if something like this was possible:

object[key1, key2, value]

I know using hashtable, you can do object[key, value]. But having two
keys, is it possible? If not using hashtables, then any other data
struture?

Thank you in advance.
Shahid


A Hashtable doesn't know anything about the key you give it except that it ais a
string, so if you have multiple keys you can concatenate them for a compound key
of sorts.

yourHashtable.ADD(Key1 + Key2, yourObject);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 14 '06 #3
Another option is a Hashtable of Hashtables, like this:

Hashtable mainTable = new Hashtable();

// Add myValue under key1, key2
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable == null)
{
subTable = new Hashtable();
mainTable[key1] = subTable;
}
subTable[key2] = myValue;

// Get myValue back, given key1 and key2
myValue = null;
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable != null)
{
myValue = (MyClass)subTable[key2];
}
// At this point, if myValue is null then it was not found; if it is
non-null then it was found.

Of course, you could create your own HashOfHashtables class that
wrapped all of this up in a nice package, including an indexer that
could take two keys and do the required lookup / assignment.

Nonetheless, this a more complicated implementation. You should use the
"concatenate the keys" method if possible, and use this only if you
have needs that the simpler solution doesn't meet (such as Larry's "get
me all the values with key1=='x'" operation).

By the way, if you use Larry's / Otis's "concatenate the keys" method,
be sure to add a delimeter in between the keys, something like this:

myHashTable.Add(key1 + "|" + key2, yourObject);

This is so that you don't run into the problem in which the keys "A"
and "BC" end up colliding with the key pair "AB" and "C". If you don't
use a delimeter (which should be outside the set of valid characters
for the key, if that's possible) then you could get collisions like
this. If you do use a delimeter then the first key would be, say,
"A|BC" and the second "AB|C" and there is no collision.

Apr 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Santosh | last post by:
Hello, I would like some input on choosing a datastructure and a algorithm. I have a text file which contains three strings(say name, phonenumber and city). The file contains a about a billion...
4
by: Prateek Basu | last post by:
Hi, Does there exist any resource (link or book) where one can find the solution for some datastructure and algorithm related problems. Pls provide the pointer. Thanks in advance.. Prateek
3
by: raju | last post by:
i am having a single linked list say 5 node are present, and one pointer that is pointing to 3rd node using this pointer only i have to come to the 1st node..... or how can i get back to the 1st...
7
by: Curious | last post by:
Hi, I have created my own data structure. Basically it is a two way 'stack', where I can push elements and pop elements both from top and bottom. I also included a counter to show the number...
0
by: lumirb | last post by:
Hello, I would like to disccuss with the following issue: We want to create database in which we can dynamically add attributes. The problem is, we do not know the datastructure before. The...
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
21
by: Pieter | last post by:
Hi, I need some type of array/list/... In which I can store objects together with a unique key. The most important thing is performance: I will need to do the whole time searches in the list of...
2
Digital Don
by: Digital Don | last post by:
Hi, Iwas trying to look for a STL datastructure in C++ where I can store structure variables and then get out value from it by just comparing the value as follows; The structure variable:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.