www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - (1.0) Clearing out an Associative Array

reply jicman <cabrera_ _wrc.xerox.com> writes:
Greetings.

Long time no write. :-)

I know that I can get the keys of an Associative array and do a foreach loop
and remove the keys of the array, but is there an easier and quicker way?  I
know that I can set any non-Associative array to [], ie,

myArr = [];

and that is sufficient or set the length to 0; but how about the Associative
arrays?  Is there a quick way of doing this?

thanks,

josť
Jan 28 2008
next sibling parent Bjoern <nanali nospam-wanadoo.fr> writes:
jicman schrieb:
 Greetings.
 
 Long time no write. :-)
 
 I know that I can get the keys of an Associative array and do a foreach loop
and remove the keys of the array, but is there an easier and quicker way?  I
know that I can set any non-Associative array to [], ie,
 
 myArr = [];
 
 and that is sufficient or set the length to 0; but how about the Associative
arrays?  Is there a quick way of doing this?
 
 thanks,
 
 josť

To clear an AA // some Associative Array properties bool hasData(T,E)(T[E] aa) { return aa.length > 0; } // by Ben Hinkle private struct BB { void*[]b; size_t nodes; } private union ToPtr(T) {T x; void * ptr; } void clear(T,E)(T[E] aa) { ToPtr!(typeof(aa)) toptr; toptr.x = aa; BB* b = cast(BB*) toptr.ptr; if (b) { b.b = null; b.nodes = 0; } } aa.clear() //D 1.x If you mean, clearing just the keys, sorry dunno Bjoern
Jan 28 2008
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and quicker 
 way?  I know that I can set any non-Associative array to [], ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

How about myArr = myArr.init; Works for both associative and standard arrays -Steve
Jan 28 2008
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Steven Schveighoffer wrote:
 "jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and quicker 
 way?  I know that I can set any non-Associative array to [], ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

How about myArr = myArr.init; Works for both associative and standard arrays -Steve

I think =null works for both too. --bb
Jan 28 2008
prev sibling parent reply jicman <cabrera_ _wrc.xerox.com> writes:
Steven Schveighoffer Wrote:

 "jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and quicker 
 way?  I know that I can set any non-Associative array to [], ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

How about myArr = myArr.init; Works for both associative and standard arrays

Thanks Steve. This one worked. josť
Jan 28 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
jicman wrote:
 Steven Schveighoffer Wrote:
 
 "jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and quicker 
 way?  I know that I can set any non-Associative array to [], ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

myArr = myArr.init; Works for both associative and standard arrays

Thanks Steve. This one worked.

Are you implying that myArr=null; did not work? because it should. And it's less typing. And avoids you having to repeat yourself. :-) --bb
Jan 29 2008
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Bill Baxter" wrote
 jicman wrote:
 Steven Schveighoffer Wrote:

 "jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and 
 quicker way?  I know that I can set any non-Associative array to [], 
 ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

myArr = myArr.init; Works for both associative and standard arrays

Thanks Steve. This one worked.

Are you implying that myArr=null; did not work? because it should. And it's less typing. And avoids you having to repeat yourself. :-)

Ha ha! jicman liked my idea better. Jealous much? j/k, I like your solution better :) -Steve
Jan 31 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:
 Ha ha! jicman liked my idea better.  Jealous much?
 j/k, I like your solution better :)

Try this: import std.stdio; void main() { int[int] a = [10:20, 30:40]; auto b = a; writefln(a, " ", b); a = null; writefln(a, " ", b); } It outputs: [10:20,30:40] [10:20,30:40] [] [10:20,30:40] Bye, bearophile
Jan 31 2008
prev sibling parent jicman <cabrera_ _wrc.xerox.com> writes:
Bill Baxter Wrote:

 jicman wrote:
 Steven Schveighoffer Wrote:
 
 "jicman" wrote
 Greetings.

 Long time no write. :-)

 I know that I can get the keys of an Associative array and do a foreach 
 loop and remove the keys of the array, but is there an easier and quicker 
 way?  I know that I can set any non-Associative array to [], ie,

 myArr = [];

 and that is sufficient or set the length to 0; but how about the 
 Associative arrays?  Is there a quick way of doing this?

 thanks,

 josť

myArr = myArr.init; Works for both associative and standard arrays

Thanks Steve. This one worked.

Are you implying that myArr=null; did not work? because it should. And it's less typing. And avoids you having to repeat yourself. :-)

No, I just tried the .init and it worked and that is what I wanted. .-)
Feb 01 2008