www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inout and foreach elements

reply "Charlie" <charles jwavro.com> writes:
Why does this not work ?

 void array_change_key_case( inout char [] [ char [] ] map)
 {

    foreach ( inout char [] key;map.keys )
   {

      key = std.string.tolower(key);

    }


 }


Charlie
Jun 04 2005
next sibling parent reply Vathix <vathix dprogramming.com> writes:
On Sat, 04 Jun 2005 17:27:22 -0400, Charlie <charles jwavro.com> wrote:

 Why does this not work ?

  void array_change_key_case( inout char [] [ char [] ] map)
  {

     foreach ( inout char [] key;map.keys )
    {

       key = std.string.tolower(key);

     }


  }
It won't recalculate the hashes or anything. You would have to remove the element and add it again with the new key, which can't be done while you're in the foreach loop.
Jun 04 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Vathix" <vathix dprogramming.com> wrote in message 
news:op.sru8qlhskcck4r esi...
 On Sat, 04 Jun 2005 17:27:22 -0400, Charlie <charles jwavro.com> wrote:

 Why does this not work ?

  void array_change_key_case( inout char [] [ char [] ] map)
  {

     foreach ( inout char [] key;map.keys )
    {

       key = std.string.tolower(key);

     }


  }
It won't recalculate the hashes or anything. You would have to remove the element and add it again with the new key, which can't be done while you're in the foreach loop.
not only that but map.keys returns a dynamic array of the keys. So the foreach code only changes the values inside the dynamic array and doesn't touch the map.
Jun 04 2005
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Sat, 4 Jun 2005 16:27:22 -0500, Charlie wrote:

 Why does this not work ?
 
  void array_change_key_case( inout char [] [ char [] ] map)
  {
 
     foreach ( inout char [] key;map.keys )
    {
 
       key = std.string.tolower(key);
 
     }
 
 
  }
Try this sort of things instead ... void array_change_key_case( inout char [] [ char [] ] map) foreach(char[] key; map.keys) { char[] newkey; newkey = tolower(key); if (newkey != key) { map[newkey] = map[key]; delete map[key]; } } } -- Derek Parnell Melbourne, Australia 5/06/2005 9:44:35 AM
Jun 04 2005