a newbie problem regarding splitter()

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 12 23:42:30 PDT 2017


On 04/12/2017 11:33 PM, alex wrote:
 > Hello,
 >
 > I've just started learning D, working my way through "The D Programming
 > Language" by Andrei Alexandrescu.

Great book but a lot has changed in D since the book was written in 
2010. Your issue is in the book's errata:

   http://erdani.com/tdpl/errata/

So, the following should work today:

import std.stdio, std.string;
import std.algorithm;

void main(){
     ulong[string] dictionary;
     foreach (line; stdin.byLine()){
         foreach (word; splitter(strip(line))){
             if (word in dictionary) continue;
             auto newID = dictionary.length;
             dictionary[word.idup] = newID;
             writeln(newID, '\t', word);
         }
     }
}

Ali



More information about the Digitalmars-d-learn mailing list