AA strange behavior

marc michel marc_member at pathlink.com
Mon Jun 12 09:10:11 PDT 2006


I found a strange behavior using AAs :

--------------------- >8 ----------------
import std.stdio;
import std.stream;

void main() {

int[][char[]] aa;

int i;
char[] s;
File f=new File("bla", FileMode.In );
while ( ! f.eof ) {
s= f.readLine();
aa[s] ~= i++;
}

f.close;

// no more luck with this :
//      aa.rehash;

foreach( char[] s, int[] i; aa ) {
writefln( "%s  =>  %d", s, i );
}

writefln("\n------------------");

// workaround :
//  while ( aa.length > 0) {

foreach( char[]s, int[] i; aa ) {
aa.remove(s);
writefln("\"%s\" => removed ",s);
}

// } 

writefln("\n------there's still : ----------");
foreach( char[] s, int[] i; aa ) {
writefln( "%s  =>  %d", s, i );
}
writefln("------END---------");
}
--------------------- >8 ----------------


with a "bla" file like this one for example :


--------------------- >8 ----------------
apple
orange
pear
strawberry
cuncumber
lemon
salad
tomato
blackberry
orange
lemon
tomato
potatoe
root
--------------------- >8 ----------------


result :

--------------------- >8 ----------------
C:\home\dev\d>aa
tomato  =>  [7,11]
strawberry  =>  [3]
blackberry  =>  [8]
orange  =>  [1,9]
potatoe  =>  [12]
root  =>  [13]
salad  =>  [6]
apple  =>  [0]
lemon  =>  [5,10]
cuncumber  =>  [4]
pear  =>  [2]

------------------
"tomato" => removed
"strawberry" => removed
"blackberry" => removed
"orange" => removed
"root" => removed
"salad" => removed
"apple" => removed
"lemon" => removed
"cuncumber" => removed
"pear" => removed

------there's still : ----------
potatoe  =>  [12]
------END---------

--------------------- >8 ----------------


Note : I also tried to add "aa.rehash" after filling aa; with no more luck.
The only workaround is to add a "while( aa.length > 0 ) " surroundind the
foreach loop which does aa.remove().


Do I need holidays ?
Does this had been discussed many times already ?







More information about the Digitalmars-d-learn mailing list