digitalmars.D.learn - Associative array key changes
- Erik Rasmussen (48/48) Mar 20 2006 I've read this
- Sean Kelly (5/9) Mar 20 2006 That's it. If you change a key value in place you must rehash the AA
- John Demme (2/14) Mar 20 2006 Actually, AAs have a .rehash method.
- Erik Rasmussen (3/21) Mar 21 2006 I know. And rehashing does nothing. Same results.
- Walter Bright (4/5) Mar 26 2006 You can't change a key in place once it's been added to an AA. Remove th...
- Andrew (6/11) Mar 26 2006 Can't is kind of a strong work there Walter. I think it's already been
- Walter Bright (3/18) Mar 27 2006 Consider it "illegal".
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
Mar 20 2006
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
Mar 20 2006
Sean Kelly wrote:Erik Rasmussen wrote:Actually, AAs have a .rehash method.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
Mar 20 2006
John Demme wrote:Sean Kelly wrote:I know. And rehashing does nothing. Same results. ErikErik Rasmussen wrote:Actually, AAs have a .rehash method.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
Mar 21 2006
"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.
Mar 26 2006
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...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, DrewI 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.
Mar 26 2006
"Andrew" <Andrew_member pathlink.com> wrote in message news:e083bb$6i8$1 digitaldaemon.com...In article <e07fcr$2e0p$1 digitaldaemon.com>, Walter Bright says...Consider it "illegal"."Erik Rasmussen" <i_am_erik yahoo.com> wrote in message news:dvog1t$2jcc$1 digitaldaemon.com...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.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.
Mar 27 2006