Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 20, 2006 Associative array key changes | ||||
---|---|---|---|---|
| ||||
I've read this (http://www.digitalmars.com/d/archives/digitalmars/D/12062.html), but there's something I don't understand. Check out the following code: --- import std.stdio; int main(char[][] args) { int[char[]] table; char[] key = "hello".dup; table[key] = 4; table["dude"] = 69; key[2] = 'r'; // table["herlo"] = 71; writefln("size: %d", table.length); foreach(char[] k; table.keys) { writef("'%s'\t-->\t", k); int* value = k in table; if(value is null) writefln("null"); else writefln(*value); } return 0; } --- If you run this, you get: --- size: 2 'dude' --> 69 'herlo' --> null --- This is almost what I would expect. I might like table["herlo"] to be 4, but I can maybe understand why it wouldn't be. But then! If you uncomment the commented line, you get this: --- size: 3 'herlo' --> 71 'dude' --> 69 'herlo' --> 71 --- Why are there two identical keys??? Why is the size 3? Maybe the lesson here is just that you should never change the values of keys inside associative arrays because the behavior will be hard to predict... Any insights out there? Cheers, Erik |
March 20, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Erik Rasmussen | Erik Rasmussen wrote:
>
> Maybe the lesson here is just that you should never change the values of keys inside associative arrays because the behavior will be hard to predict...
That's it. If you change a key value in place you must rehash the AA before it can be used. I don't think there's any way to do this using the built-in AA functionality, though it would be easy enough to add.
Sean
|
March 20, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> Erik Rasmussen wrote:
>>
>> Maybe the lesson here is just that you should never change the values of keys inside associative arrays because the behavior will be hard to predict...
>
> That's it. If you change a key value in place you must rehash the AA before it can be used. I don't think there's any way to do this using the built-in AA functionality, though it would be easy enough to add.
>
>
> Sean
Actually, AAs have a .rehash method.
|
March 21, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme | John Demme wrote:
> Sean Kelly wrote:
>
>
>>Erik Rasmussen wrote:
>>
>>>Maybe the lesson here is just that you should never change the values of
>>>keys inside associative arrays because the behavior will be hard to
>>>predict...
>>
>>That's it. If you change a key value in place you must rehash the AA
>>before it can be used. I don't think there's any way to do this using
>>the built-in AA functionality, though it would be easy enough to add.
>>
>>
>>Sean
>
>
> Actually, AAs have a .rehash method.
I know. And rehashing does nothing. Same results.
Erik
|
March 27, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Erik Rasmussen | "Erik Rasmussen" <i_am_erik@yahoo.com> wrote in message news:dvog1t$2jcc$1@digitaldaemon.com... > I know. And rehashing does nothing. Same results. You can't change a key in place once it's been added to an AA. Remove the key from the AA, then add a new one. |
March 27, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | In article <e07fcr$2e0p$1@digitaldaemon.com>, Walter Bright says... > > >"Erik Rasmussen" <i_am_erik@yahoo.com> wrote in message news:dvog1t$2jcc$1@digitaldaemon.com... >> I know. And rehashing does nothing. Same results. > >You can't change a key in place once it's been added to an AA. Remove the key from the AA, then add a new one. > > Can't is kind of a strong work there Walter. I think it's already been demonstrated that you can (albeit with undesired results). What we need from you now is to either make it "safely" legal to do so or "explicitly" illegal. Thanks, Drew |
March 27, 2006 Re: Associative array key changes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew | "Andrew" <Andrew_member@pathlink.com> wrote in message news:e083bb$6i8$1@digitaldaemon.com... > In article <e07fcr$2e0p$1@digitaldaemon.com>, Walter Bright says... >> >> >>"Erik Rasmussen" <i_am_erik@yahoo.com> wrote in message news:dvog1t$2jcc$1@digitaldaemon.com... >>> I know. And rehashing does nothing. Same results. >> >>You can't change a key in place once it's been added to an AA. Remove the key from the AA, then add a new one. >> >> > > Can't is kind of a strong work there Walter. I think it's already been > demonstrated that you can (albeit with undesired results). What we need > from you > now is to either make it "safely" legal to do so or "explicitly" illegal. Consider it "illegal". |
Copyright © 1999-2021 by the D Language Foundation