string comparison

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Dec 20 06:01:23 PST 2010


On Sun, 19 Dec 2010 07:01:30 +0000, doubleagent wrote:

> Andrei's quick dictionary illustration [in his book, 'The D Programming
> Language'] doesn't seem to work.  Code attached.

That's strange.  I ran the example you posted using DMD 2.050 myself, and 
it works for me.  Are you 100% sure that you are running this version, 
and that it is not using an outdated Phobos version (from an older 
installation, for instance)?

One suggestion:  Try replacing the next-to-last line with this:

  dictionary[word.idup] = newId;

The 'word' array is mutable and reused by byLine() on each iteration.  By 
doing the above you use an immutable copy of it as the key instead.


> On my computer, with d2-0.5.0, I got the following output while testing.
> 
> andrei
> 0	andrei
>  andrei
> 1	andrei
> 
> 
> Also, why doesn't 'splitter' show up on the site's documentation of
> std.string?  And what advantage does 'splitter(strip(line))' offer over
> 'split(line)'?

splitter is defined in std.algorithm.  The fact that it becomes visible 
when you import std.string is due to bug 314:

  http://d.puremagic.com/issues/show_bug.cgi?id=314

(std.string is supposed to publically import just a few symbols from 
std.algorithm, but because of this bug the whole module gets imported 
publically.)

The advantage with splitter is that it is lazy and therefore more 
efficient.  split() is eager and allocates memory to hold the string 
fragments.

-Lars


More information about the Digitalmars-d-learn mailing list