www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array for array of classes

reply clayasaurus <clayasaurus gmail.com> writes:
Ok, for basic types I know I can use

int[char[]] assoc;
assoc["3"] = 3;

And for classes I can do.

Class[char[]] assoc;
assoc["3"] = new Class;

Now, lets say I want to test if assoc["3"] has already been initialized 
before I allocate it, but testing it with

if (assoc["3"] is null)
   assoc["3"] = new Class;

I get a seg fault. Any other way to do this? File attached to better 
illustrate my problem. Thanks.

~ Clay
Apr 26 2006
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 26 Apr 2006 23:49:07 -0500, clayasaurus wrote:

 import std.stdio;
 
 int main()
 {
 	char[] i = "3";
 
 	int[char[]] assoc;
 	assoc[i] = 3;
 
 	Class[char[]] assoc2;
 
 	// doesn't work
 	if (assoc2[i] is null)
 		assoc2[i] = new Class;
 
 	// works
 	assoc2[i] = new Class;
 
 	writefln("hi");
 
 	return 0;
 }
 
 class Class
 {
   public:
 	this(){}
 	int x,y;
 }
Try this ... if (!(i in assoc2)) assoc2[i] = new Class; -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 27/04/2006 3:09:47 PM
Apr 26 2006
parent clayasaurus <clayasaurus gmail.com> writes:
Derek Parnell wrote:
 On Wed, 26 Apr 2006 23:49:07 -0500, clayasaurus wrote:
 
 Try this ...
 
   if (!(i in assoc2))
     assoc2[i] = new Class;
 
Thanks :) That seems to do the trick.
Apr 26 2006
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
clayasaurus wrote:
<snip>
 Class[char[]] assoc;
 assoc["3"] = new Class;
 
 Now, lets say I want to test if assoc["3"] has already been initialized 
 before I allocate it, but testing it with
 
 if (assoc["3"] is null)
   assoc["3"] = new Class;
 
 I get a seg fault.
<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
parent reply "Alberto Simon" <lugaidster gmail.com> writes:
You should use '(("3" in assoc) == null)' instead of '(assoc["3"] is null)'

Regards,
Alberto Simon
"Stewart Gordon" <smjg_1998 yahoo.com> escribió en el mensaje 
news:e2qh6d$1lfk$2 digitaldaemon.com...
 clayasaurus wrote:
 <snip>
 Class[char[]] assoc;
 assoc["3"] = new Class;

 Now, lets say I want to test if assoc["3"] has already been initialized 
 before I allocate it, but testing it with

 if (assoc["3"] is null)
   assoc["3"] = new Class;

 I get a seg fault.
<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
parent reply Sean Kelly <sean f4.ca> writes:
The correct use of AAs is so intuitive I'm amazed anyone ever gets it 
wrong.  It's times like this when I wish I'd been more vocal about 
actually liking the old syntax :-p


Alberto Simon wrote:
 You should use '(("3" in assoc) == null)' instead of '(assoc["3"] is null)'
 
 Regards,
 Alberto Simon
 "Stewart Gordon" <smjg_1998 yahoo.com> escribió en el mensaje 
 news:e2qh6d$1lfk$2 digitaldaemon.com...
 clayasaurus wrote:
 <snip>
 Class[char[]] assoc;
 assoc["3"] = new Class;

 Now, lets say I want to test if assoc["3"] has already been initialized 
 before I allocate it, but testing it with

 if (assoc["3"] is null)
   assoc["3"] = new Class;

 I get a seg fault.
<snip> That's strange. It should be an ArrayBoundsError. Stewart.
Apr 27 2006
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Sean Kelly wrote:
 The correct use of AAs is so intuitive I'm amazed anyone ever gets it 
 wrong.  It's times like this when I wish I'd been more vocal about 
 actually liking the old syntax :-p
<snip top of upside-down reply> What old syntax? The in operator has always been the correct way to check whether an AA contains a given key. Looking it up and comparing it against valuetype.init was never a general solution. And if ("3" in assoc) or if (!("3" in assoc)) still works just the same. [F'up back to d.D.learn] Stewart.
Apr 28 2006