www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - New to D

reply Mark <harmonic33 yahoo.ca> writes:
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()) {


splitter


of splitter ??

         if(word in dictionary) continue;



         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!
Oct 21 2016
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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()) {




 splitter ??

         if(word in dictionary) continue;



         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/
Oct 21 2016
parent reply Mark <harmonic33 yahoo.ca> writes:
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.
Oct 21 2016
next sibling parent Mike Parker <aldacron gmail.com> writes:
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" :)
Oct 21 2016
prev sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Oct 22 2016
parent reply Mike Parker <aldacron gmail.com> writes:
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];
Oct 22 2016
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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 :)
Oct 23 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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()) {




 splitter ??

         if(word in dictionary) continue;



         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
Oct 25 2016
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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.
Oct 26 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Oct 27 2016
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Thursday, 27 October 2016 at 13:43:26 UTC, Steven 
Schveighoffer wrote:
 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.
Depends. I recall experimenting early on with Memory mapped files (80Mb-300Mb), and it instantly loaded, no time at all. I don't think it even read the file or portions until I made requests to it (Course in my instance I'd have to create virtual records to access everything, still need to re-write and finish that project). It really depends on the circumstances though, and I suppose also understanding when a buffer is shared/reused and when to use dup.
Oct 27 2016