www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative array key changes

reply Erik Rasmussen <i_am_erik yahoo.com> writes:
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
parent reply Sean Kelly <sean f4.ca> writes:
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
parent reply John Demme <me teqdruid.com> writes:
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.
Mar 20 2006
parent reply Erik Rasmussen <i_am_erik yahoo.com> writes:
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
Mar 21 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"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
parent reply Andrew <Andrew_member pathlink.com> writes:
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
Mar 26 2006
parent "Walter Bright" <newshound digitalmars.com> writes:
"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".
Mar 27 2006