www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - clear AAs

reply Mandel <mandel mailinator.com> writes:
Hi,

I have a simple question, how can I remove all elements of an associative array
in an elegant way?
The length property of an AA is read only.
Doing a foreach/remove seems a bit to much text; most containers provide some
clear method.
Apr 17 2007
parent reply Bradley Smith <digitalmars-com baysmith.com> writes:
Mandel wrote:
 Hi,
 
 I have a simple question, how can I remove all elements of an associative array
 in an elegant way?
 The length property of an AA is read only.
 Doing a foreach/remove seems a bit to much text; most containers provide some
clear method.
import std.stdio; void main() { bool[char[]] aa; aa["abc"] = true; aa["def"] = false; writefln(aa); aa = aa.init; writefln(aa); }
Apr 17 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Bradley Smith wrote:
 Mandel wrote:
 Hi,

 I have a simple question, how can I remove all elements of an
 associative array
 in an elegant way?
 The length property of an AA is read only.
 Doing a foreach/remove seems a bit to much text; most containers
 provide some clear method.
import std.stdio; void main() { bool[char[]] aa; aa["abc"] = true; aa["def"] = false; writefln(aa); aa = aa.init; writefln(aa); }
... that's so simple; why didn't I think of that before?! Thanks for that! :) -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 18 2007
parent reply Max Samukha <samukha voliacable.com> writes:
On Wed, 18 Apr 2007 18:01:00 +1000, Daniel Keep
<daniel.keep.lists gmail.com> wrote:

Bradley Smith wrote:
 Mandel wrote:
 Hi,

 I have a simple question, how can I remove all elements of an
 associative array
 in an elegant way?
 The length property of an AA is read only.
 Doing a foreach/remove seems a bit to much text; most containers
 provide some clear method.
import std.stdio; void main() { bool[char[]] aa; aa["abc"] = true; aa["def"] = false; writefln(aa); aa = aa.init; writefln(aa); }
... that's so simple; why didn't I think of that before?! Thanks for that! :) -- Daniel
Or aa = null (aa.init == null)
Apr 18 2007
parent reply BCS <BCS pathlink.com> writes:
Max Samukha wrote:
 Daniel Keep wrote:
 
Bradley Smith wrote:

import std.stdio;

void main() {

  bool[char[]] aa;
  aa["abc"] = true;
  aa["def"] = false;
  writefln(aa);

  aa = aa.init;
  writefln(aa);
}
... that's so simple; why didn't I think of that before?! Thanks for that! :) -- Daniel
Or aa = null (aa.init == null)
can you then fill it up again?
Apr 18 2007
parent Max Samukha <samukha voliacable.com> writes:
On Wed, 18 Apr 2007 09:18:50 -0700, BCS <BCS pathlink.com> wrote:

Max Samukha wrote:
 Daniel Keep wrote:
 
Bradley Smith wrote:

import std.stdio;

void main() {

  bool[char[]] aa;
  aa["abc"] = true;
  aa["def"] = false;
  writefln(aa);

  aa = aa.init;
  writefln(aa);
}
... that's so simple; why didn't I think of that before?! Thanks for that! :) -- Daniel
Or aa = null (aa.init == null)
can you then fill it up again?
Yes
Apr 18 2007
prev sibling parent Aarti_pl <aarti interia.pl> writes:
Bradley Smith napisaƂ(a):
 Mandel wrote:
 Hi,

 I have a simple question, how can I remove all elements of an 
 associative array
 in an elegant way?
 The length property of an AA is read only.
 Doing a foreach/remove seems a bit to much text; most containers 
 provide some clear method.
import std.stdio; void main() { bool[char[]] aa; aa["abc"] = true; aa["def"] = false; writefln(aa); aa = aa.init; writefln(aa); }
You can also do it in following way: aa = null; BR Marcin Kuszczak aarti_pl
Apr 18 2007