Thread overview
inout and foreach elements
Jun 04, 2005
Charlie
Jun 04, 2005
Vathix
Jun 04, 2005
Ben Hinkle
Jun 04, 2005
Derek Parnell
June 04, 2005
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


June 04, 2005
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.
June 04, 2005
"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.


June 04, 2005
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