Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
June 16, 2014 Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Hello guys, as first I don't know, if I'm writing to correct section, but I've got a problem. I'm actually reading book of D guide and trying to do it like it is in book. My code is: import std.stdio, std.string; void main() { uint[string] 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); } } } And I'm getting this - Error: undefined identifier splitter It seems like std.string doesn't contain splitter. |
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanios | Add "import std.algorithm". Splitter is defined there. http://dlang.org/phobos/std_algorithm.html#splitter On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote: > Hello guys, as first I don't know, if I'm writing to correct > section, but I've got a problem. I'm actually reading book of D > guide and trying to do it like it is in book. > > My code is: > > import std.stdio, std.string; > > void main() { > uint[string] 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); > } > } > } > > And I'm getting this - Error: undefined identifier splitter > It seems like std.string doesn't contain splitter. |
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanios | On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote: > > <snip> > > And I'm getting this - Error: undefined identifier splitter > It seems like std.string doesn't contain splitter. You can find the solution to this and other issues you may hit in the errata: http://erdani.com/tdpl/errata/ |
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tolga Cakiroglu | Thanks, but getting another error. auto newID = dictionary.length; Error: associative arrays can only be assigned values with immutable keys, not char[] |
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanios | I think you can find splitter in std.array. I had a few other
problems compiling your code, I could get this version to work:
import std.stdio, std.array, std.string; //need to import
std.array
void main() {
ulong[string] dictionary; // the length property is ulong, not
uint
foreach (line; stdin.byLine()) {
foreach (word; splitter(strip(line))) {
if (word in dictionary) continue;
auto newID = dictionary.length;
dictionary[word.idup] = newID; //dictionarys need immutable
keys, you can create this with .idup
writeln(newID, '\t', word);
}
}
}
Good luck!
Andrew
On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote:
> Hello guys, as first I don't know, if I'm writing to correct
> section, but I've got a problem. I'm actually reading book of D
> guide and trying to do it like it is in book.
>
> My code is:
>
> import std.stdio, std.string;
>
> void main() {
> uint[string] 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);
> }
> }
> }
>
> And I'm getting this - Error: undefined identifier splitter
> It seems like std.string doesn't contain splitter.
|
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | On Monday, 16 June 2014 at 16:42:01 UTC, Brad Anderson wrote:
> On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote:
>>
>> <snip>
>>
>> And I'm getting this - Error: undefined identifier splitter
>> It seems like std.string doesn't contain splitter.
>
> You can find the solution to this and other issues you may hit in the errata:
>
> http://erdani.com/tdpl/errata/
Thanks, I've solved it!
|
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Brown | Sorry, comments split over two lines, this should work:
import std.stdio, std.array, std.string; //need to import
std.array
void main() {
ulong[string] dictionary; // the length property is ulong, not
uint
foreach (line; stdin.byLine()) {
foreach (word; splitter(strip(line))) {
if (word in dictionary) continue;
auto newID = dictionary.length; //dictionarys need
immutable keys, you // can create this with .idup
dictionary[word.idup] = newID;
writeln(newID, '\t', word);
}
}
}
On Monday, 16 June 2014 at 16:46:37 UTC, Andrew Brown wrote:
> I think you can find splitter in std.array. I had a few other
> problems compiling your code, I could get this version to work:
>
> import std.stdio, std.array, std.string; //need to import
> std.array
>
> void main() {
> ulong[string] dictionary; // the length property is ulong, not
> uint
> foreach (line; stdin.byLine()) {
> foreach (word; splitter(strip(line))) {
> if (word in dictionary) continue;
> auto newID = dictionary.length;
> dictionary[word.idup] = newID; //dictionarys need immutable
> keys, you can create this with .idup
> writeln(newID, '\t', word);
> }
> }
> }
>
> Good luck!
>
> Andrew
>
> On Monday, 16 June 2014 at 16:38:15 UTC, Sanios wrote:
>> Hello guys, as first I don't know, if I'm writing to correct
>> section, but I've got a problem. I'm actually reading book of D
>> guide and trying to do it like it is in book.
>>
>> My code is:
>>
>> import std.stdio, std.string;
>>
>> void main() {
>> uint[string] 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);
>> }
>> }
>> }
>>
>> And I'm getting this - Error: undefined identifier splitter
>> It seems like std.string doesn't contain splitter.
|
June 16, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Brown | I'm giving up
On Monday, 16 June 2014 at 16:49:46 UTC, Andrew Brown wrote:
> Sorry, comments split over two lines, this should work:
>
> import std.stdio, std.array, std.string; //need to import
> std.array
>
> void main() {
> ulong[string] dictionary; // the length property is ulong, not
> uint
> foreach (line; stdin.byLine()) {
> foreach (word; splitter(strip(line))) {
> if (word in dictionary) continue;
> auto newID = dictionary.length; //dictionarys need
> immutable keys, you // can create this with .idup
> dictionary[word.idup] = newID;
> writeln(newID, '\t', word);
> }
> }
> }
|
June 19, 2014 Re: Doing exercise from book, but I'm getting error with splitter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Brown | On Monday, 16 June 2014 at 16:49:46 UTC, Andrew Brown wrote:
> ulong[string] dictionary; // the length property is ulong, not
> uint
Actually length is size_t (uint on x86 and ulong on x64).
|
Copyright © 1999-2021 by the D Language Foundation