digitalmars.D.learn - Merging two hashes
- Andrej Mitrovic (14/14) Jan 20 2012 Is there a way to merge the keys from one hash to another (overwriting
- Steven Schveighoffer (5/19) Jan 20 2012 You can in dcollections :)
- Andrej Mitrovic (2/25) Jan 20 2012
Is there a way to merge the keys from one hash to another (overwriting
any duplicates) without using a foreach loop? E.g.:
void main()
{
    int[int] a, b;
    a[0] = 0;
    b[1] = 1;
    b += a;  // ?
}
It's not too hard to write this of course:
    foreach (key, val; a)
        b[key] = val;
But I'm wondering if an enhancement request is in order, maybe an
operator overload, or maybe a special merge function.
 Jan 20 2012
On Fri, 20 Jan 2012 12:46:40 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:
 Is there a way to merge the keys from one hash to another (overwriting
 any duplicates) without using a foreach loop? E.g.:
 void main()
 {
     int[int] a, b;
     a[0] = 0;
     b[1] = 1;
     b += a;  // ?
 }
 It's not too hard to write this of course:
     foreach (key, val; a)
         b[key] = val;
 But I'm wondering if an enhancement request is in order, maybe an
 operator overload, or maybe a special merge function.
You can in dcollections :)
http://www.dsource.org/projects/dcollections/browser/branches/d2/dcollections/model/Map.d#L40
-Steve
 Jan 20 2012
Nice! On 1/20/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:On Fri, 20 Jan 2012 12:46:40 -0500, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:Is there a way to merge the keys from one hash to another (overwriting any duplicates) without using a foreach loop? E.g.: void main() { int[int] a, b; a[0] = 0; b[1] = 1; b += a; // ? } It's not too hard to write this of course: foreach (key, val; a) b[key] = val; But I'm wondering if an enhancement request is in order, maybe an operator overload, or maybe a special merge function.You can in dcollections :) http://www.dsource.org/projects/dcollections/browser/branches/d2/dcollections/model/Map.d#L40 -Steve
 Jan 20 2012








 
  
  
  Andrej Mitrovic <andrej.mitrovich gmail.com>
 Andrej Mitrovic <andrej.mitrovich gmail.com>