digitalmars.D.learn - New to D
- Mark (38/38) Oct 21 2016 Hello, Im a 3rd year Comp Sci student in Edmonton Alberta, Canada.
- rikki cattermole (7/43) Oct 21 2016 Things have changed since TDPL was created but here is the errata which
- Mark (6/6) Oct 21 2016 Thanks for the fast reply.
- Mike Parker (9/15) Oct 21 2016 For simple single file experiments like this, I strongly
- Daniel Kozak via Digitalmars-d-learn (6/12) Oct 22 2016 uint[string] dictionary;
- Mike Parker (3/9) Oct 22 2016 I believe you meant:
- Daniel Kozak via Digitalmars-d-learn (2/11) Oct 23 2016 Yes :)
- Steven Schveighoffer (12/39) Oct 25 2016 I will note, that in addition to the other comments, this is going to
- Era Scarecrow (6/15) Oct 26 2016 If there's a case where you have immutable data AND can
- Steven Schveighoffer (8/20) Oct 27 2016 It depends on the size of the file and the expectation of duplicate
- Era Scarecrow (11/19) Oct 27 2016 Depends. I recall experimenting early on with Memory mapped
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
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
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
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
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
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_tI believe you meant: size_t[string];
Oct 22 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:Yes :)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_tI believe you meant: size_t[string];
Oct 23 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()) { 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
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
On 10/27/16 2:40 AM, Era Scarecrow wrote:On Tuesday, 25 October 2016 at 14:40:17 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. -SteveI 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.
Oct 27 2016
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