www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - associative array and function

reply Zarathustra <adam.chrapkowski gmail.com> writes:
only input data is modified

module main;
import std.stdio;
import std.process;

void Function(in uint[int]o_a){
  foreach(l_a; o_a.keys)o_a.remove(l_a);
}

void
main(char [][] args){
  uint[int]l_a;
  l_a[1] = 0;
  l_a[2] = 3;
  writefln(l_a.length);  // 2
  Function(l_a);
  writefln(l_a.length);  // 0
  system("pause");
}
Aug 01 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Zarathustra,

 only input data is modified
 

are you asking a question or pointing out an issue or just replying to that last thread?
 module main;
 import std.stdio;
 import std.process;
 void Function(in uint[int]o_a){
 foreach(l_a; o_a.keys)o_a.remove(l_a);
 }
 void
 main(char [][] args){
 uint[int]l_a;
 l_a[1] = 0;
 l_a[2] = 3;
 writefln(l_a.length);  // 2
 Function(l_a);
 writefln(l_a.length);  // 0
 system("pause");
 }

Aug 01 2008
parent reply Zarathustra <adam.chrapkowski gmail.com> writes:
BCS Wrote:
are you asking a question or pointing out an issue or just replying to that
last thread?


Is it a issue? What do you think about it?
Aug 01 2008
parent BCS <ao pathlink.com> writes:
Reply to Zarathustra,

 only input data is modified
 
 module main;
 import std.stdio;
 import std.process;
 void Function(in uint[int]o_a){
 foreach(l_a; o_a.keys)o_a.remove(l_a);
 }
 void
 main(char [][] args){
 uint[int]l_a;
 l_a[1] = 0;
 l_a[2] = 3;
 writefln(l_a.length);  // 2
 Function(l_a);
 writefln(l_a.length);  // 0
 system("pause");
 }

That act's as I would expect. (AA's are reference objects) If you want to not effect the AA on the outside then set it to null (better check that) on the inside function. If you want to remove only some things and only do /that/ on the inside, your sunk.
Aug 01 2008