www.digitalmars.com         C & C++   DMDScript  

D - Associative Array Question

reply Terry Bayne <gnome hiwaay.net> writes:
If I have an associative array of a user defined type (code shown below), 
what is returned if I attempt to look up a value with a key that is not 
found in the associative array?  In the function main() shown below, what 
would be the value returned for Test2, in the statement:

    	Test2 = H["Test2"];

In other words what happens when the key which is passed as an index to 
an associative array is not a valid key?

Thanks
Terry

// ====== Code Follows ========



enum TypeTag { Str, Int, Long};

struct _DataItem
{
	TypeTag Type;
	str Value;
};
typedef _DataItem DataItem;

struct _HashData
{
	str Name;
	DataItem[str] Data;
}
typedef _HashData HashData;

void main()
{
    	HashData H;
    	DataItem Test1;
    	DataItem Test2;

    	Test1.Type = Str;
    	Test1.Value = "Test1";
    	H["Test1"] = Test1;
    	Test2 = H["Test2"];
}
Jul 28 2003
parent "Walter" <walter digitalmars.com> writes:
A new entry for it is created. If you want to see if a value is in or not,
do:
    if ("Test2" in H)

"Terry Bayne" <gnome hiwaay.net> wrote in message
news:Xns93C6663511098tbaynehiwaaynet 63.105.9.61...
 If I have an associative array of a user defined type (code shown below),
 what is returned if I attempt to look up a value with a key that is not
 found in the associative array?  In the function main() shown below, what
 would be the value returned for Test2, in the statement:

     Test2 = H["Test2"];

 In other words what happens when the key which is passed as an index to
 an associative array is not a valid key?

 Thanks
 Terry

 // ====== Code Follows ========



 enum TypeTag { Str, Int, Long};

 struct _DataItem
 {
 TypeTag Type;
 str Value;
 };
 typedef _DataItem DataItem;

 struct _HashData
 {
 str Name;
 DataItem[str] Data;
 }
 typedef _HashData HashData;

 void main()
 {
     HashData H;
     DataItem Test1;
     DataItem Test2;

     Test1.Type = Str;
     Test1.Value = "Test1";
     H["Test1"] = Test1;
     Test2 = H["Test2"];
 }
Jul 28 2003