www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - TDPL dictionary example - error

--000e0cd3329aa25edf049821335e
Content-Type: text/plain; charset=ISO-8859-1

Greetings,

I just joined here, so sorry if this has been posted before.

I'm reading TDPL and the example on page 8 doesn't compile.  I'm using the
latest GDC with GCC 4.4.5.  I've checked the errata, but nothing for this
error.

import std.stdio;
import std.string;

void main(){

     size_t[char[]] dictionary;
    foreach(line; stdin.byLine()){
              foreach(word; splitter(strip(line))){
                          if(word in dictionary) continue;
                    auto newID = dictionary.length;
                          dictionary[word] = newID;
                    writeln(newID, '\t', word);
            }
    }
    //writeln(dictionary.length);
}


This is the error:
dictionary.d:12: Error: associative arrays can only be assigned values with
immutable keys, not char[]

If i use immutable keys, it works, but it defeats the purpose.  So what's
wrong with the code?

--000e0cd3329aa25edf049821335e
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Greetings,<br><br>I just joined here, so sorry if this has been posted befo=
re.<br><br>I&#39;m reading TDPL and the example on page 8 doesn&#39;t compi=
le.=A0 I&#39;m using the latest GDC with GCC 4.4.5.=A0 I&#39;ve checked the=
 errata, but nothing for this error.<br>
<br>import std.stdio;<br>import std.string;<br><br>void main(){<br><br>=A0=
=A0=A0=A0 size_t[char[]] dictionary;<br>=A0=A0=A0 foreach(line; stdin.byLin=
e()){<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0 foreach(word; splitter(strip(l=
ine))){<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=
=A0 if(word in dictionary) continue;<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 auto newID =3D dictionary=
.length;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=
=A0 dictionary[word] =3D newID;<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =
=A0=A0=A0 writeln(newID, &#39;\t&#39;, word);<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=
=A0 }<br>=A0=A0=A0 }<br>=A0=A0=A0 //writeln(dictionary.length);<br>
}<br><br><br>This is the error:<br>dictionary.d:12: Error: associative arra=
ys can only be assigned values with immutable keys, not char[]<br><br>If i =
use immutable keys, it works, but it defeats the purpose.=A0 So what&#39;s =
wrong with the code?<br>

--000e0cd3329aa25edf049821335e--
Dec 23 2010