www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delay function?

reply Brendan <brenzie gmail.com> writes:
I looked through the Phobos page, but haven't found anything like it. Is there
a sort of delay function anywhere in Phobos?
Sep 02 2008
parent reply Brendan <brenzie gmail.com> writes:
Brendan Wrote:

 I looked through the Phobos page, but haven't found anything like it. Is there
a sort of delay function anywhere in Phobos?
Well, I just used 'sleep' from the Linux shell instead. I don't understand yet what I'm doing wrong with the following function, though. Could someone explain and advise? void scrollString( char[] s ) { for ( int i = 0; i < s.length; i++ ) writef( s[i] ); system( "sleep 0.2" ); }
Sep 02 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
Content-Disposition: inline

On Tue, Sep 2, 2008 at 7:11 AM, Brendan <brenzie gmail.com> wrote:

 Brendan Wrote:

 I looked through the Phobos page, but haven't found anything like it. Is
there a sort of delay function anywhere in Phobos? Well, I just used 'sleep' from the Linux shell instead. I don't understand yet what I'm doing wrong with the following function, though. Could someone explain and advise? void scrollString( char[] s ) { for ( int i = 0; i < s.length; i++ ) writef( s[i] ); system( "sleep 0.2" ); }
If you're expecting it to pause after each character.. you're missing braces around the body of the for loop, this isn't Python ;)
Sep 02 2008
parent reply Brendan <brenzie gmail.com> writes:
Jarrett Billingsley Wrote:


 </blockquote></div><br>If you're expecting it to pause after each character..
you're missing braces around the body of the for loop, this isn't Python
;)<br></div>
 
Oooooh, what a foolish mistake! How could I forget that? Thanks for pointing it out. But the result isn't fruitful, though. When I run it, all I get is an empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea?
Sep 02 2008
next sibling parent reply "Chris R. Miller" <lordSaurontheGreat gmail.com> writes:
Brendan wrote:
 Jarrett Billingsley Wrote:
=20
=20
 </blockquote></div><br>If you're expecting it to pause after each char=
acter.. you're missing braces around the body of the for loop, this isn't= Python ;)<br></div>

=20
 Oooooh, what a foolish mistake! How could I forget that? Thanks for poi=
nting it out.
=20
 But the result isn't fruitful, though. When I run it, all I get is an e=
mpty screen, then after many seconds the whole string is displayed at onc= e (not to mention I set the sleep time to 0.1 seconds). I'm probably miss= ing something. Any idea? Flush the output?
Sep 02 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
Content-Disposition: inline

On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
lordSaurontheGreat gmail.com> wrote:

 Brendan wrote:
 Jarrett Billingsley Wrote:


 </blockquote></div><br>If you're expecting it to pause after each
character.. you're missing braces around the body of the for loop, this isn't Python ;)<br></div>

 Oooooh, what a foolish mistake! How could I forget that? Thanks for
pointing it out.
 But the result isn't fruitful, though. When I run it, all I get is an
empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea? Flush the output?
Totally. foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
Sep 02 2008
parent reply Brendan <brenzie gmail.com> writes:
Jarrett Billingsley Wrote:

 On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
 lordSaurontheGreat gmail.com> wrote:
 
 Brendan wrote:
 Jarrett Billingsley Wrote:


 </blockquote></div><br>If you're expecting it to pause after each
character.. you're missing braces around the body of the for loop, this isn't Python ;)<br></div>

 Oooooh, what a foolish mistake! How could I forget that? Thanks for
pointing it out.
 But the result isn't fruitful, though. When I run it, all I get is an
empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea? Flush the output?
Totally. foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
Sep 02 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Brendan,

 Jarrett Billingsley Wrote:
 
 On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
 lordSaurontheGreat gmail.com> wrote:
 Brendan wrote:
 
 Jarrett Billingsley Wrote:
 
 </blockquote></div><br>If you're expecting it to pause after each
 
character.. you're missing braces around the body of the for loop, this isn't Python ;)<br></div>
 Oooooh, what a foolish mistake! How could I forget that? Thanks for
 
pointing it out.
 But the result isn't fruitful, though. When I run it, all I get is
 an
 
empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea? Flush the output?
Totally. foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
The act of making the screen show text is rather costly in time, so generally libs buffer your output, often until a newline or some preset amount. Flush just forces a buffer flush and the resultant output.
Sep 02 2008
parent reply Brendan <brenzie gmail.com> writes:
BCS Wrote:

 Reply to Brendan,
 
 Jarrett Billingsley Wrote:
 
 On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
 lordSaurontheGreat gmail.com> wrote:
 Brendan wrote:
 
 Jarrett Billingsley Wrote:
 
 </blockquote></div><br>If you're expecting it to pause after each
 
character.. you're missing braces around the body of the for loop, this isn't Python ;)<br></div>
 Oooooh, what a foolish mistake! How could I forget that? Thanks for
 
pointing it out.
 But the result isn't fruitful, though. When I run it, all I get is
 an
 
empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea? Flush the output?
Totally. foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
The act of making the screen show text is rather costly in time, so generally libs buffer your output, often until a newline or some preset amount. Flush just forces a buffer flush and the resultant output.
Thanks for your responses, BCS. This is the first time I'm kind of communicating through a newsgroup such as this, as I'm normally used to a forum. Why are your two messages separate from the thread?
Sep 02 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Brendan,

 Thanks for your responses, BCS. This is the first time I'm kind of
 communicating through a newsgroup such as this, as I'm normally used
 to a forum. Why are your two messages separate from the thread?
 
If you are using the D web site interface, because it's broken (and has been for years). Best approach is to get a "real" newsgroup client program, thunderbird works, I use Omea by jetbrains and IIRC Outlook also does newsgroups.
Sep 02 2008
parent Brendan <brenzie gmail.com> writes:
BCS wrote:
 Reply to Brendan,
 
 Thanks for your responses, BCS. This is the first time I'm kind of
 communicating through a newsgroup such as this, as I'm normally used
 to a forum. Why are your two messages separate from the thread?
If you are using the D web site interface, because it's broken (and has been for years). Best approach is to get a "real" newsgroup client program, thunderbird works, I use Omea by jetbrains and IIRC Outlook also does newsgroups.
It's a lot better now, as I'm using Thunderbird. Thanks for the tip :)
Sep 03 2008
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 03 Sep 2008 00:24:02 +0400, Brendan <brenzie gmail.com> wrote:

 Jarrett Billingsley Wrote:

 On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
 lordSaurontheGreat gmail.com> wrote:

 Brendan wrote:
 Jarrett Billingsley Wrote:


 </blockquote></div><br>If you're expecting it to pause after each
character.. you're missing braces around the body of the for loop,
this
 isn't Python ;)<br></div>

 Oooooh, what a foolish mistake! How could I forget that? Thanks for
pointing it out.
 But the result isn't fruitful, though. When I run it, all I get is  
an
 empty screen, then after many seconds the whole string is displayed  
at once
 (not to mention I set the sleep time to 0.1 seconds). I'm probably  
missing
 something. Any idea?

 Flush the output?
Totally. foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
Google for "buffering". When you print something, it doesn't get displayed immediately. Instead, the string is copied to some internal buffer and when it gets full (or close to full) it is then flushed, i.e. its contents copied to the console and buffer size reset. Something like this: char[] buffer; void writefln(char[] str) { buffer ~= str; if (buffer.length > 1024) { doTheRealPrinting(buffer); buffer.length = 0; } }
Sep 02 2008
parent Brendan <brenzie gmail.com> writes:
Denis Koroskin Wrote:

 Google for "buffering". When you print something, it doesn't get displayed  
 immediately. Instead, the string is copied to some internal buffer and  
 when it gets full (or close to full) it is then flushed, i.e. its contents  
 copied to the console and buffer size reset. Something like this:
 
 char[] buffer;
 
 void writefln(char[] str)
 {
    buffer ~= str;
    if (buffer.length > 1024) {
      doTheRealPrinting(buffer);
      buffer.length = 0;
    }
 }
Ahhh, a buffer! So basically, in the correct piece of code (the scroll function) a few messages back, in the for loop, the buffer is constantly unloading the characters from itself? It is so beautiful. Thanks :)
Sep 02 2008
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Brendan:
 Oooooh, what a foolish mistake! How could I forget that? Thanks for pointing
it out.
It's a mistake, but in the past years I have seen several similar mistakes in real programs written in C-like languages. That's why I'm a fan of semantically significant indentations :-) (Or of a built-in lint-like capability of the language that signals such indentation errors are syntax errors). Bye, bearophile
Sep 02 2008
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"bearophile" wrote
 Brendan:
 Oooooh, what a foolish mistake! How could I forget that? Thanks for 
 pointing it out.
It's a mistake, but in the past years I have seen several similar mistakes in real programs written in C-like languages. That's why I'm a fan of semantically significant indentations :-) (Or of a built-in lint-like capability of the language that signals such indentation errors are syntax errors).
Any reasonable syntax highlighting editor should show you that you forgot something. In vim, if I forget something like this, the second statement's indent jumps back to the same level as the if statement, signalling that I forgot to add the curly brace. -Steve
Sep 02 2008
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Brendan,

 Brendan Wrote:
 
 I looked through the Phobos page, but haven't found anything like it.
 Is there a sort of delay function anywhere in Phobos?
 
Well, I just used 'sleep' from the Linux shell instead. I don't understand yet what I'm doing wrong with the following function, though. Could someone explain and advise? void scrollString( char[] s ) { for ( int i = 0; i < s.length; i++ ) writef( s[i] ); system( "sleep 0.2" ); }
IIRC the are the standard sleep and usleep functions in std.c.something
Sep 02 2008