www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problems with DPL example.

reply %u <foo bar.com> writes:
Hello.  I'm having problems compiling the following:

// From chapter 1 of D Programming Language.
//
import std.stdio, std.string;

void main() {
  uint[string] dictionary;

  foreach( line; stdin.byLine()) {
    // Break sentence into words
    // Add each word in the sentence to the vocabulary
    foreach( word; splitter(strip(line))) {
      if( word in dictionary) continue; // Nothing to do.
      auto newID = dictionary.length;
      dictionary[word] = newID;
      writeln( newid, '\t', word);
    }
  }
  return;
}



$ dmd wordcount.d
wordcount.d(9): Error: undefined identifier splitter



$ dmd -v
DMD32 D Compiler v2.055
Copyright (c) 1999-2011 by Digital Mars written by Walter Bright
Documentation: http://www.digitalmars.com/d/2.0/index.html


I am doing the examples in cygwin.

Anyone know what the problem is?


thanks.
Oct 10 2011
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On 10.10.2011 19:55, %u wrote:
 Hello.  I'm having problems compiling the following:

 // From chapter 1 of D Programming Language.
 //
 import std.stdio, std.string;

 void main() {
    uint[string] dictionary;

    foreach( line; stdin.byLine()) {
      // Break sentence into words
      // Add each word in the sentence to the vocabulary
      foreach( word; splitter(strip(line))) {
        if( word in dictionary) continue; // Nothing to do.
        auto newID = dictionary.length;
        dictionary[word] = newID;
        writeln( newid, '\t', word);
      }
    }
    return;
 }



 $ dmd wordcount.d
 wordcount.d(9): Error: undefined identifier splitter



 $ dmd -v
 DMD32 D Compiler v2.055
 Copyright (c) 1999-2011 by Digital Mars written by Walter Bright
 Documentation: http://www.digitalmars.com/d/2.0/index.html


 I am doing the examples in cygwin.

 Anyone know what the problem is?


 thanks.
Seems some functionality was moved in 2.052. From std.string documentation: "IMPORTANT NOTE: Beginning with version 2.052, the following symbols have been generalized beyond strings and moved to different modules." And "split Use std.array.split instead" std.array includes both split and splitter. http://www.d-programming-language.org/phobos/std_array.html#split
Oct 10 2011
parent reply %u <foo.bar yahoo.com> writes:
== Quote from simendsjo (simendsjo gmail.com)'s article
 Seems some functionality was moved in 2.052. From std.string
documentation:
 "IMPORTANT NOTE: Beginning with version 2.052, the following
symbols
 have been generalized beyond strings and moved to different
modules."
 And
 "split	Use std.array.split instead"
 std.array includes both split and splitter.
 http://www.d-programming-language.org/phobos/std_array.html#split
Okay, thanks for that. I added the extra module, and found I made a typo. So I corrected that too. Anyways, I am now having another problem. What can I do to fix it? : import std.stdio, std.string, std.array; void main() { uint[string] dictionary; foreach( line; stdin.byLine()) { // Break sentence into words // Add each word in the sentence to the vocabulary foreach( word; splitter(strip(line))) { if( word in dictionary) continue; // Nothing to do. auto newID = dictionary.length; dictionary[word] = newID; writeln( newID, '\t', word); } } return; } $ dmd wordcount.d wordcount.d(12): Error: associative arrays can only be assigned values with immutable keys, not char[] $ dmd -v DMD32 D Compiler v2.055 Copyright (c) 1999-2011 by Digital Mars written by Walter Bright Documentation: http://www.digitalmars.com/d/2.0/index.html Usage: thanks!
Oct 10 2011
parent reply Justin Whear <justin economicmodeling.com> writes:
You need to create an immutable copy of word before using it as a key. That 
is, replace this line:

       dictionary[word] = newID;
with dictionary[word.idup] = newID;
Oct 10 2011
parent reply %u <foo bar.com> writes:
Thanks. It works, but I get something weird in the output.  I get
the problem if I run it in a dos prompt or in a cygwin prompt:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D>echo hello | wordcount2.exe
0       hello
std.stdio.StdioException std\stdio.d(2156): Bad file descriptor
----------------
42A910
42A787
40318A
40239C
402141
403798
4037D7
4033D3
465D71
----------------


Do you know what this is caused by?

thanks.

Here's the code again:


import std.stdio, std.string, std.array;

void main() {
  uint[string] dictionary;

  foreach( line; stdin.byLine()) {
    // Break sentence into words
    // Add each word in the sentence to the vocabulary
    foreach( word; splitter(strip(line))) {
      if( word in dictionary) continue; // Nothing to do.
      auto newID = dictionary.length;
      dictionary[word.idup] = newID;
      writeln( newID, '\t', word);
    }
  }
  return;
}


thanks.
Oct 10 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
%u:

 D>echo hello | wordcount2.exe < wordcount2.d
 0       hello
 std.stdio.StdioException std\stdio.d(2156): Bad file descriptor
Try: wordcount2.exe < wordcount2.d Bye, bearophile
Oct 10 2011
parent reply simendsjo <simendsjo gmail.com> writes:
On 10.10.2011 21:38, bearophile wrote:
 %u:

 D>echo hello | wordcount2.exe<  wordcount2.d
 0       hello
 std.stdio.StdioException std\stdio.d(2156): Bad file descriptor
Try: wordcount2.exe< wordcount2.d Bye, bearophile
Shouldn't the original way work too? Another point: I recommend compiling with debug symbols as it gives you a nice stacktrace.
Oct 10 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
simendsjo:

 Shouldn't the original way work too?
I don't remember.
 Another point: I recommend compiling with debug symbols as it gives you 
 a nice stacktrace.
I think debug symbols should be present on default, to produce a nice stack trace on default, and be disabled with a compiler switch :-) Bye, bearophile
Oct 10 2011
parent %u <foo bar.com> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 simendsjo:
 Shouldn't the original way work too?
I don't remember.
 Another point: I recommend compiling with debug symbols as it
gives you
 a nice stacktrace.
I think debug symbols should be present on default, to produce a
nice stack trace on default, and be disabled with a compiler switch :-)
 Bye,
 bearophile
If I use file indirection instead of piping output to the d program, it works in cygwin window. I'm not a dos expert, so I don't know how to do the same test on windows. anyways, thanks!
Oct 10 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 10 Oct 2011 15:42:26 -0400, simendsjo <simendsjo gmail.com> wrote:

 On 10.10.2011 21:38, bearophile wrote:
 %u:

 D>echo hello | wordcount2.exe<  wordcount2.d
 0       hello
 std.stdio.StdioException std\stdio.d(2156): Bad file descriptor
This is a bug in the C runtime that D uses, where pipes are not used correctly. I'm working on getting Walter to fix it as it's needed for the new std.process as well.
 Try:

 wordcount2.exe<  wordcount2.d

 Bye,
 bearophile
Shouldn't the original way work too?
Yes, but the main difference is, bearophile's method simply opens a file, which FILE * obviously supports no problem. Your method creates a process with a pipe as the input. -Steve
Oct 12 2011
parent "Shripad K" <assortmentofsorts gmail.com> writes:
Boy its really hard to navigate this forum for an old thread.
Seems like a lot has changed in D since this thread. Here is the 
correct snippet if someone comes searching for "error: undefined 
identifier splitter" like me :) (using DMD64 D Compiler v2.058)

import std.stdio, std.string, 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);
		}
	}
}

On Wednesday, 12 October 2011 at 15:39:14 UTC, Steven 
Schveighoffer wrote:
 On Mon, 10 Oct 2011 15:42:26 -0400, simendsjo 
 <simendsjo gmail.com> wrote:

 On 10.10.2011 21:38, bearophile wrote:
 %u:

 D>echo hello | wordcount2.exe<  wordcount2.d
 0       hello
 std.stdio.StdioException std\stdio.d(2156): Bad file 
 descriptor
This is a bug in the C runtime that D uses, where pipes are not used correctly. I'm working on getting Walter to fix it as it's needed for the new std.process as well.
 Try:

 wordcount2.exe<  wordcount2.d

 Bye,
 bearophile
Shouldn't the original way work too?
Yes, but the main difference is, bearophile's method simply opens a file, which FILE * obviously supports no problem. Your method creates a process with a pipe as the input. -Steve
Mar 13 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
You should checkout out this page: http://erdani.com/tdpl/errata/

- Jonathan M Davis
Oct 10 2011