Associative array issue

Igor Kolesnik shadowmaan at gmail.com
Wed Jan 23 12:07:24 PST 2013


Hi;

I'm trying to run an example from the tutorial on 
http://www.informit.com/articles/article.aspx?p=1381876&seqNum=4
Here is the code

import std.stdio, std.string;

void main() {
   uint[string] dic;
   foreach (line; stdin.byLine) {
     string[] words = cast(string[])split(strip(line));
     foreach (word; words) {
       if (word in dic)
	continue;
       uint id = dic.length;
       dic[word] = id;
       writeln(id, '\t', word);
     }
   }
   //foreach (k,v; dic)
   //  writeln(k, '|', v);
}

When run it behaves somehow strange. Here is an example of the 
input/output I get

the type of array
0       the
1       type
2       of
3       array
in d the type of array
4       in
5       d
6       the
7       type
8       of
9       array

It seems like the 'word in dic' doesn't find the item in the 
array.
If I print the contents of 'dic' array on exit, I get the 
following

d|5
in |0
e of |3
in|4
the|6
array|9
  the|1
type|7
ty|2
of|8

Can someone help me understand what is going wrong? Am I missing 
something here?

ps: mdm32 v2.061 on Win7 x64

Sincerely,
Igor


More information about the Digitalmars-d-learn mailing list