Thread overview
(1.0) Clearing out an Associative Array
Jan 28, 2008
jicman
Jan 28, 2008
Bjoern
Jan 28, 2008
Bill Baxter
Jan 29, 2008
jicman
Jan 29, 2008
Bill Baxter
Feb 01, 2008
bearophile
Feb 01, 2008
jicman
January 28, 2008
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é
January 28, 2008
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
January 28, 2008
"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


January 28, 2008
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
January 29, 2008
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é
January 29, 2008
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é
>> How about
>>
>> 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
February 01, 2008
"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é
>>> How about
>>>
>>> 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


February 01, 2008
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
February 01, 2008
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é
> >> How about
> >>
> >> 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. .-)