digitalmars.D.learn - Read integer from console and sleep
- Iain (26/26) May 04 2012 I tried to use the sleep function from the Rosetta Code project:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (37/62) May 04 2012 You can use spaces in the format string to read and ignore all
- bearophile (14/25) May 04 2012 If you want Iain I'll put this version by Ali Çehreli in
- Iain (5/18) May 04 2012 That's great, and thanks to Ali for explaining.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (23/47) May 04 2012 As far as I know, people maintain sites individually.
- bearophile (4/6) May 04 2012 http://d.puremagic.com/issues/show_bug.cgi?id=8040
- bearophile (6/8) May 04 2012 In the last weeks I have rewritten/fixed/updated more than 480 D
- Iain (5/13) May 09 2012 Wow! It's people like you who are making D understandable for
I tried to use the sleep function from the Rosetta Code project: http://rosettacode.org/wiki/Sleep#D Am I right in thinking this is D1 code, as it will not compile under 2.059? So, I tried writing my own version, and hit my head against many brick walls along the way. My result takes more lines than C, Could anyone explain how to optimise the code below before I post it to Rosetta? ----------------- import std.stdio; import std.conv; import std.string; import core.thread; void main() { write("Enter a time to sleep (in seconds): "); string input = stdin.readln(); // have to make this a string first, it can't go directly into parse! auto time = parse!long(input); // have to use parse!, as to! crashes when it sees a \n writeln("Sleeping..."); Thread.sleep( dur!("seconds")(time) ); writeln("Awake!"); } Many thanks!
May 04 2012
On 05/04/2012 06:18 AM, Iain wrote:I tried to use the sleep function from the Rosetta Code project: http://rosettacode.org/wiki/Sleep#D Am I right in thinking this is D1 code, as it will not compile under2.059?So, I tried writing my own version, and hit my head against many brick I'm guessing it can't be the best solution. Could anyone explain how to optimise the code below before I post it to Rosetta? ----------------- import std.stdio; import std.conv; import std.string; import core.thread; void main() { write("Enter a time to sleep (in seconds): "); string input = stdin.readln(); // have to make this a string first, it can't go directly into parse! auto time = parse!long(input); // have to use parse!, as to! crashes when it sees a \n writeln("Sleeping..."); Thread.sleep( dur!("seconds")(time) ); writeln("Awake!"); } Many thanks!You can use spaces in the format string to read and ignore all whitespace. This one ignores the '\n' and others that may have been left over from the previous read (I know that there can't be any in this small program): import std.stdio; import core.thread; void main() { write("Enter a time to sleep (in seconds): "); long time; readf(" %s", &time); writeln("Sleeping..."); Thread.sleep( dur!("seconds")(time) ); writeln("Awake!"); } Some people have developed their own input modules. Reading any type can be as simple as in the following: import std.stdio; import core.thread; T read(T)(string prompt) { T value; write(prompt); readf(" %s", &value); return value; } void main() { immutable time = read!long("Enter a time to sleep (in seconds): "); writeln("Sleeping..."); Thread.sleep(dur!("seconds")(time)); writeln("Awake!"); } Ali
May 04 2012
Ali Çehreli:import std.stdio; import core.thread; void main() { write("Enter a time to sleep (in seconds): "); long time; readf(" %s", &time); writeln("Sleeping..."); Thread.sleep( dur!("seconds")(time) ); writeln("Awake!"); }If you want Iain I'll put this version by Ali Çehreli in Rosettacode: import std.stdio, core.thread; void main() { write("Enter a time to sleep (in seconds): "); long secs; readf(" %d", &secs); writeln("Sleeping..."); Thread.sleep(dur!"seconds"(secs)); writeln("Awake!"); } Bye, bearophile
May 04 2012
If you want Iain I'll put this version by Ali Çehreli in Rosettacode: import std.stdio, core.thread; void main() { write("Enter a time to sleep (in seconds): "); long secs; readf(" %d", &secs); writeln("Sleeping..."); Thread.sleep(dur!"seconds"(secs)); writeln("Awake!"); } Bye, bearophileThat's great, and thanks to Ali for explaining. Out of interest, is there a coordinated effort to update things like Rosetta or the dsource.org tutorials to use D2.0? I find one of the big stumbling blocks to learning is that half of the tutorials and code snippets out there are obsolete!
May 04 2012
On 05/04/2012 10:51 AM, Iain wrote:As far as I know, people maintain sites individually. It is unfortunate but not surprising that many sites have been outdated considering the amount of changes to D in the recent years. I had to rewrite a number of chapters and go over many examples since I have started the original book a number of years ago. (See my shiny new signature. ;)) Somewhat off-topic, I have just hit a broken example today when translating the "null and is" chapter. This used to print 0: writeln(null); but now causes an obscure compilation error: .../dmd/phobos/std/stdio.d(1562): Error: undefined identifier 'length' .../dmd/phobos/std/stdio.d(1562): Error: undefined identifier 'ptr', did you mean 'template tr(C1,C2,C3,C4 = immutable(char))'? deneme.d(76953): Error: template instance std.stdio.writeln!(typeof(null)) error instantiating It takes the 'null' value to be a "Specialization for strings - a very frequent case". This is a bug, right? But what should printing 'null' do anyway? Print 0 as before, or "null"? Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.htmlIf you want Iain I'll put this version by Ali Çehreli in Rosettacode: import std.stdio, core.thread; void main() { write("Enter a time to sleep (in seconds): "); long secs; readf(" %d", &secs); writeln("Sleeping..."); Thread.sleep(dur!"seconds"(secs)); writeln("Awake!"); } Bye, bearophileThat's great, and thanks to Ali for explaining. Out of interest, is there a coordinated effort to update things like Rosetta or the dsource.org tutorials to use D2.0? I find one of the big stumbling blocks to learning is that half of the tutorials and code snippets out there are obsolete!
May 04 2012
Ali Çehreli:This is a bug, right? But what should printing 'null' do anyway? Print 0 as before, or "null"?http://d.puremagic.com/issues/show_bug.cgi?id=8040 Bye, bearophile
May 04 2012
Iain:Out of interest, is there a coordinated effort to update things like Rosetta or the dsource.org tutorials to use D2.0?In the last weeks I have rewritten/fixed/updated more than 480 D entries (some Rosettacode Tasks have more than one D implementation). But there are about 110 entries left to update. Bye, bearophile
May 04 2012
On Friday, 4 May 2012 at 19:37:40 UTC, bearophile wrote:Iain:Wow! It's people like you who are making D understandable for the rest of us. Thank you! As a testament to my learning, I have updated http://rosettacode.org/wiki/User_input/Text#DOut of interest, is there a coordinated effort to update things like Rosetta or the dsource.org tutorials to use D2.0?In the last weeks I have rewritten/fixed/updated more than 480 D entries (some Rosettacode Tasks have more than one D implementation). But there are about 110 entries left to update. Bye, bearophile
May 09 2012