Jump to page: 1 2
Thread overview
New to D
Oct 22, 2016
Mark
Oct 22, 2016
rikki cattermole
Oct 22, 2016
Mark
Oct 22, 2016
Mike Parker
Oct 22, 2016
Daniel Kozak
Oct 22, 2016
Mike Parker
Oct 23, 2016
Daniel Kozak
Oct 27, 2016
Era Scarecrow
Oct 27, 2016
Era Scarecrow
October 22, 2016
Hello, Im a 3rd year Comp Sci student in Edmonton Alberta, Canada.

Ive learned how to use C, and dabbled in C++ in school. Im also in a Oop course using Java.

I picked up the book The D Programming Language by Alexrei Alexandrescu a few years ago.
Lately Im really wanting to get into D, as It seems like a better version of C, and feels like java in a way.



However;

Ive run into a bit of a problem while writing some code. Im not sure if Im doing something wrong, or maybe the things Im using are depreciated??

Code Blocks does not give any messages as to what is going wrong. I haven't configured anything, and am not familiar with messing around with IDE settings.

Its directly based off of the example on page 8.

here is the code that does not compile:


import std.stdio, std.string;

void main() { ... }

void dict() {

    uint[string] dictionary;

    foreach(line; stdin.byLine()) {

        //chunk = splitter(strip(line); ## errors because of splitter

        foreach(word; splitter(strip(line))) { ## errors because of splitter ??

        if(word in dictionary) continue;

        //writeln(dictionary.length);  ## gives 0 ??

        auto newID = dictionary.length;
        dictionary[word] = newId;


        writeln(newID, ' ', word);

        }
    }

}




Im not sure what Im doing wrong here.

modifying the code to just print the result of splitLines results in the entire line.
and just having splitter give a sting to print also errors.

Please help, thanks!

October 22, 2016
On 22/10/2016 6:25 PM, Mark wrote:
> Hello, Im a 3rd year Comp Sci student in Edmonton Alberta, Canada.
>
> Ive learned how to use C, and dabbled in C++ in school. Im also in a Oop
> course using Java.
>
> I picked up the book The D Programming Language by Alexrei Alexandrescu
> a few years ago.
> Lately Im really wanting to get into D, as It seems like a better
> version of C, and feels like java in a way.

Things have changed since TDPL was created but here is the errata which says what[0].

> However;
>
> Ive run into a bit of a problem while writing some code. Im not sure if
> Im doing something wrong, or maybe the things Im using are depreciated??
>
> Code Blocks does not give any messages as to what is going wrong. I
> haven't configured anything, and am not familiar with messing around
> with IDE settings.
>
> Its directly based off of the example on page 8.
>
> here is the code that does not compile:
>
>
> import std.stdio, std.string;
>
> void main() { ... }
>
> void dict() {
>
>     uint[string] dictionary;
>
>     foreach(line; stdin.byLine()) {
>
>         //chunk = splitter(strip(line); ## errors because of splitter
>
>         foreach(word; splitter(strip(line))) { ## errors because of
> splitter ??
>
>         if(word in dictionary) continue;
>
>         //writeln(dictionary.length);  ## gives 0 ??
>
>         auto newID = dictionary.length;
>         dictionary[word] = newId;
>
>
>         writeln(newID, ' ', word);
>
>         }
>     }
>
> }
>
>
>
>
> Im not sure what Im doing wrong here.
>
> modifying the code to just print the result of splitLines results in the
> entire line.
> and just having splitter give a sting to print also errors.
>
> Please help, thanks!

Oh splitter is in std.algorithm.iteration[1].
Import it and it should work.

[0] http://erdani.com/tdpl/errata/
[1] http://dlang.org/phobos/std_algorithm_iteration.html#.splitter

October 22, 2016
Thanks for the fast reply.

That did work. But now the error is on the line:

         dictionary[word] = newId;

I changed the value to 10, still errors. ??


everything else is as before.

thanks.
October 22, 2016
On Saturday, 22 October 2016 at 05:41:34 UTC, Mark wrote:
> Thanks for the fast reply.
>
> That did work. But now the error is on the line:
>
>          dictionary[word] = newId;
>
> I changed the value to 10, still errors. ??
>
>
> everything else is as before.
>
> thanks.

For simple single file experiments like this, I strongly recommend you ditch Code::Blocks (or any IDE) and just use a text editor + the command line. All you need is the compiler on the path, then you can do this:

dmd foo.d

Any errors will be shown right in the console. Try that out then come back and post the error messages you see. Preferably something more informative than "the error is on the line" :)
October 22, 2016
Dne 22.10.2016 v 07:41 Mark via Digitalmars-d-learn napsal(a):

> Thanks for the fast reply.
>
> That did work. But now the error is on the line:
>
>          dictionary[word] = newId;
>
> I changed the value to 10, still errors. ??
>
>
> everything else is as before.
>
> thanks.
uint[string] dictionary;
should be
uint[size_t] dictionary;

because size_t is 32bit on x86 system and 64bit on x86_64
and you are trying to put array length to dictionary which is size_t
October 22, 2016
On Saturday, 22 October 2016 at 08:05:12 UTC, Daniel Kozak wrote:

> uint[string] dictionary;
> should be
> uint[size_t] dictionary;
>
> because size_t is 32bit on x86 system and 64bit on x86_64
> and you are trying to put array length to dictionary which is size_t

I believe you meant:

size_t[string];
October 23, 2016
Dne 22.10.2016 v 11:04 Mike Parker via Digitalmars-d-learn napsal(a):

> On Saturday, 22 October 2016 at 08:05:12 UTC, Daniel Kozak wrote:
>
>> uint[string] dictionary;
>> should be
>> uint[size_t] dictionary;
>>
>> because size_t is 32bit on x86 system and 64bit on x86_64
>> and you are trying to put array length to dictionary which is size_t
>
> I believe you meant:
>
> size_t[string];
Yes :)
October 25, 2016
On 10/22/16 1:25 AM, Mark wrote:
> Hello, Im a 3rd year Comp Sci student in Edmonton Alberta, Canada.
>
> Ive learned how to use C, and dabbled in C++ in school. Im also in a Oop
> course using Java.
>
> I picked up the book The D Programming Language by Alexrei Alexandrescu
> a few years ago.
> Lately Im really wanting to get into D, as It seems like a better
> version of C, and feels like java in a way.
>
>
>
> However;
>
> Ive run into a bit of a problem while writing some code. Im not sure if
> Im doing something wrong, or maybe the things Im using are depreciated??
>
> Code Blocks does not give any messages as to what is going wrong. I
> haven't configured anything, and am not familiar with messing around
> with IDE settings.
>
> Its directly based off of the example on page 8.
>
> here is the code that does not compile:
>
>
> import std.stdio, std.string;
>
> void main() { ... }
>
> void dict() {
>
>     uint[string] dictionary;
>
>     foreach(line; stdin.byLine()) {
>
>         //chunk = splitter(strip(line); ## errors because of splitter
>
>         foreach(word; splitter(strip(line))) { ## errors because of
> splitter ??
>
>         if(word in dictionary) continue;
>
>         //writeln(dictionary.length);  ## gives 0 ??
>
>         auto newID = dictionary.length;
>         dictionary[word] = newId;

I will note, that in addition to the other comments, this is going to result in corruption. Simply put, the buffer that `line` uses is reused for each line. So the string data used inside the associative array is going to change. This will result in not finding words already added when using the `word in dictionary` check.

You need to use dictionary[word.idup] = newId; This will duplicate the line into a GC string that will live as long as the AA uses it.

Alternatively, you can use byLineCopy, but this needlessly copies the line for each iteration, when you may find all the words in the line are already added.

-Steve
October 27, 2016
On Tuesday, 25 October 2016 at 14:40:17 UTC, Steven Schveighoffer wrote:
> I will note, that in addition to the other comments, this is going to result in corruption. Simply put, the buffer that 'line' uses is reused for each line. So the string data used inside the associative array is going to change. This will result in not finding words already added when using the 'word in dictionary' check.
>
> You need to use dictionary[word.idup] = newId; This will duplicate the line into a GC string that will live as long as the AA uses it.

 If there's a case where you have immutable data AND can reference it (say... mmap files?) then referencing the string would work rather than having to duplicate it.

 However it isn't going to work with stdin input and in this case.
October 27, 2016
On 10/27/16 2:40 AM, Era Scarecrow wrote:
> On Tuesday, 25 October 2016 at 14:40:17 UTC, Steven Schveighoffer wrote:
>> I will note, that in addition to the other comments, this is going to
>> result in corruption. Simply put, the buffer that 'line' uses is
>> reused for each line. So the string data used inside the associative
>> array is going to change. This will result in not finding words
>> already added when using the 'word in dictionary' check.
>>
>> You need to use dictionary[word.idup] = newId; This will duplicate the
>> line into a GC string that will live as long as the AA uses it.
>
>  If there's a case where you have immutable data AND can reference it
> (say... mmap files?) then referencing the string would work rather than
> having to duplicate it.

It depends on the size of the file and the expectation of duplicate words. I'm assuming the number of words is limited, so you are going to allocate far less data by duping on demand. In addition, you may incur penalties for accessing the string directly from the file -- the OS may have swapped out that page and have to re-read it from the file itself.

You could also read the entire file into a string and go based on that.

-Steve
« First   ‹ Prev
1 2