Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Counting (easy!)

    I'm sure this is easy but I'm a newbie. I was doing control statements (for,
    while,etc.) like this:

    for ($count = 1; $count <= 5; $count++) {
    print "$count\n";
    }

    What I wanted to do was to make each number appear in sequence like you see
    in a countdown (or up, in this case) instead of all on the screen at once.
    I've tried using sleep but I'm not getting anywhere with it. Any ideas?
    Also, any methods or ideas on how to approach creating code in general? For
    example, what's the thought process for trying to do what I want to do? I've
    tried pseudo code and it's helped some but then I hit the wall and don't how
    to go further. Thanks!

    __________________________________________________ _______________
    Frustrated with dial-up? Get high-speed for as low as $26.95.
    [url]https://broadband.msn.com[/url] (Prices may vary by service area.)

    Trent Rigsbee Guest

  2. Similar Questions and Discussions

    1. Counting (easy!) - select???
      Christiane Nerz wrote: It's used in this case for sleeping for less than one second. The sleep() function only sleeps for a whole number of...
    2. search for email address in file, was Counting (easy!)
      Please choose a subject that is reflective of your post and start a new thread when appropriate... would I The @ in the above does need to be...
    3. Counting (easy!) (YES!!)
      I think I figured it out! A FIRST!! for ($i = 1; $i <= 5; $i++){ sleep 1; print "$i\n"; } I prints out like this: 1...2...3...4...5
    4. Easy question = easy answer?
      Well, so I'm pretty new with Freehand, at the mo running with MX and havin' a problem (not big, but anyway)... I'm creating some cards and they...
    5. Easy Question/Easy Answer
      Ok, this is all i want to know how to do, i made a text link, now when someone rolls over it with their pointer i want it to change color. Simple?
  3. #2

    Default RE: Counting (easy!)


    Try using the "\b" character to erase your output.

    -----Original Message-----
    From: Trent Rigsbee [mailto:sweetdaddysiki@hotmail.com]
    Sent: Wednesday, November 12, 2003 5:06 PM
    To: [email]beginners@perl.org[/email]
    Subject: Counting (easy!)

    I'm sure this is easy but I'm a newbie. I was doing control statements
    (for,
    while,etc.) like this:

    for ($count = 1; $count <= 5; $count++) {
    print "$count\n"; }

    What I wanted to do was to make each number appear in sequence like you
    see in a countdown (or up, in this case) instead of all on the screen at
    once.
    I've tried using sleep but I'm not getting anywhere with it. Any ideas?
    Also, any methods or ideas on how to approach creating code in general?
    For example, what's the thought process for trying to do what I want to
    do? I've tried pseudo code and it's helped some but then I hit the wall
    and don't how to go further. Thanks!

    __________________________________________________ _______________
    Frustrated with dial-up? Get high-speed for as low as $26.95.
    [url]https://broadband.msn.com[/url] (Prices may vary by service area.)


    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email] For additional
    commands, e-mail: [email]beginners-help@perl.org[/email]

    Tim Johnson Guest

  4. #3

    Default Re: Counting (easy!)

    Trent Rigsbee wrote:
    > I'm sure this is easy but I'm a newbie. I was doing control statements
    > (for, while,etc.) like this:
    >
    > for ($count = 1; $count <= 5; $count++) {
    > print "$count\n";
    > }
    >
    > What I wanted to do was to make each number appear in sequence like you
    > see in a countdown (or up, in this case) instead of all on the screen at
    > once. I've tried using sleep but I'm not getting anywhere with it. Any
    > ideas? Also, any methods or ideas on how to approach creating code in
    > general? For example, what's the thought process for trying to do what I
    > want to do? I've tried pseudo code and it's helped some but then I hit the
    > wall and don't how to go further. Thanks!
    like count down from 5 to 1 slowly in a single row? try:

    [panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'

    david
    --
    $_='015001450154015401570040016701570162015401440'
    ,*,=*|=*_,split+local$";map{~$_&1&&{$,<<=1,$#.=qq~
    /63968e72w28@_[$_..$||3])=>~}}0..s,.,,g-01;*_=*#,s
    [[s/eval+w/.`9`/e]]n\\xng.print+eval(eval"qq`$_`")
    David Guest

  5. #4

    Default Re: Counting (easy!)

    On Wed, 2003-11-12 at 20:05, Trent Rigsbee wrote:
    > I'm sure this is easy but I'm a newbie. I was doing control statements (for,
    > while,etc.) like this:
    >
    > for ($count = 1; $count <= 5; $count++) {
    > print "$count\n";
    > }
    >
    > What I wanted to do was to make each number appear in sequence like you see
    > in a countdown (or up, in this case) instead of all on the screen at once.
    > I've tried using sleep but I'm not getting anywhere with it. Any ideas?
    > Also, any methods or ideas on how to approach creating code in general? For
    > example, what's the thought process for trying to do what I want to do? I've
    > tried pseudo code and it's helped some but then I hit the wall and don't how
    > to go further. Thanks!
    >
    Try this code....either of these might be what you're looking for

    #!/usr/bin/perl
    #
    use warnings;
    use strict;

    print "printing 1thru5 with buffering\n";
    select undef, undef, undef, 0.25 or print $_
    for 1 .. 5;
    print "\nflush forced\n";

    $|++;
    print "printing 1thru5 without buffering\n";
    select undef, undef, undef, 0.25 or print $_
    for 1 .. 5;
    print "\nflush forced\n";


    Hope this helps,
    --
    Kevin Old <kold@kold.homelinux.com>

    Kevin Old Guest

  6. #5

    Default Re: Counting (easy!)


    On Wednesday, Nov 12, 2003, at 18:07 US/Pacific, david wrote:
    [..]
    >
    > like count down from 5 to 1 slowly in a single row? try:
    >
    > [panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'
    >
    > david
    Minor Nit, that "\r" will not actually go out

    54321

    rather pleasantly returns the cursor to the first position
    since you were polite enough to leave a space out in front!

    Also a lovely way of helping the OP move from 'c style'

    for( my $i=1; $i <= 5; $i++)

    into the more natural perlish

    for(N..M)

    construct, but you really should have forewarned him a
    bit about our habit of the compound statement before
    the interative, vice the more easy to 'fix'
    my $max = 6;
    my $sleeptime=1;
    for(1..5)
    {
    print " @{[$max-$_]}\r" ;
    sleep($sleeptime);
    }

    but props for the golf tip.....

    Any help with my slice, I keep putting the ball
    way off to the other fairway....

    ciao
    drieux

    ---

    Drieux Guest

  7. #6

    Default Re: Counting (easy!)

    On Thu, 13 Nov 2003 01:05:37 +0000, Trent Rigsbee wrote:
    > for ($count = 1; $count <= 5; $count++) {
    > print "$count\n";
    > }
    This is very C'ish. In Perl we tend to:

    for ( 1..5 ) {
    print $_ . "\n";
    # sleep( 1 );
    }

    Uncomment the sleep() thing if you want Perl to sleep for 1 second for
    each iteration. Try 'perldoc -f sleep' for more information on sleep().
    > What I wanted to do was to make each number appear in sequence like you
    > see in a countdown (or up, in this case) instead of all on the screen at
    > once.
    I'm not sure what you really mean, but if you want to replace the previous
    number with the new one, you need to send some backspaces (\b) to STDOUT;

    my $from = 1;
    my $to = 60;
    my $prev = 0;

    $| = 1; # We don't want to buffer the output

    for ( $from .. $to ) {
    print $_;
    sleep( 1 );
    print "\b" x length($prev);
    $prev = $_;
    }

    If you really need your application to display some information on its
    progress, you really should check out Term::ProgressBar on CPAN:

    [url]http://www.cpan.org/[/url]
    > Also, any methods or ideas on how to approach creating code in general?
    1. Learn Perl.
    2. Try to do something.
    3. If #2 fails, read any documentation you can find.
    4. comp.lang.perl.misc


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

  8. #7

    Default Re: Counting (easy!)

    Trent Rigsbee wrote:
    >
    > I'm sure this is easy but I'm a newbie. I was doing control statements (for,
    > while,etc.) like this:
    >
    > for ($count = 1; $count <= 5; $count++) {
    > print "$count\n";
    > }
    In Perl that is usually written as:

    for $count ( 1 .. 5 ) {
    print "$count\n";
    }

    > What I wanted to do was to make each number appear in sequence like you see
    > in a countdown (or up, in this case) instead of all on the screen at once.
    In most consoles/terminals standard output (STDOUT) is line buffered.

    man 3 stdout
    [snip]
    The stream stderr is unbuffered. The stream stdout is line-buffered
    when
    it points to a terminal. Partial lines will not appear until
    fflush(3) or
    exit(3) is called, or a newline is printed. This can produce
    unexpected
    results, especially with debugging output.

    man 3 setbuf
    [snip]
    The three types of buffering available are unbuffered,
    block buffered, and line buffered. When an output stream
    is unbuffered, information appears on the destination file
    or terminal as soon as written; when it is block buffered
    many characters are saved up and written as a block; when
    it is line buffered characters are saved up until a new*
    line is output or input is read from any stream attached
    to a terminal device (typically stdin).


    Since you are printing a newline after $count the value of $count should
    appear on your screen as soon as it is available. You can force STDOUT
    to automatically flush its buffer by setting the autoflush variable to a
    non-zero value before using STDOUT.

    $| = 1; # turn on autoflush
    for $count ( 1 .. 5 ) {
    print "$count\n";
    }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  9. #8

    Default Re: Counting (easy!) - select???

    quite interesting chunk of code - but what the hell does select does here?
    Yeah - I rtfm - but didn't understand it - maybe one could explain it in
    more simple words?
    Jane

    Kevin Old wrote:

    .....
    >>What I wanted to do was to make each number appear in sequence like you see
    >>in a countdown (or up, in this case) instead of all on the screen at once.
    .....


    > Try this code....either of these might be what you're looking for
    >
    > #!/usr/bin/perl
    > #
    > use warnings;
    > use strict;
    >
    > print "printing 1thru5 with buffering\n";
    > select undef, undef, undef, 0.25 or print $_
    > for 1 .. 5;
    > print "\nflush forced\n";
    >
    > $|++;
    > print "printing 1thru5 without buffering\n";
    > select undef, undef, undef, 0.25 or print $_
    > for 1 .. 5;
    > print "\nflush forced\n";
    >
    >
    > Hope this helps,
    >


    Christiane Nerz Guest

  10. #9

    Default RE: Counting (easy!)

    > In Perl that is usually written as:
    >
    > for $count ( 1 .. 5 ) {
    > print "$count\n";
    > }
    Or even easier:
    for(1..5) { print; }
    Or if you nee the newline:
    for(1..5) { print "$_\n"; }

    HTH

    DMuey
    >
    Dan Muey Guest

  11. #10

    Default Re: Counting (easy!)

    Dan Muey wrote:
    >
    > >
    > > In Perl that is usually written as:
    > >
    > > for $count ( 1 .. 5 ) {
    > > print "$count\n";
    > > }
    >
    > Or even easier:
    > for(1..5) { print; }
    > Or if you nee the newline:
    > for(1..5) { print "$_\n"; }
    If we're competing, then there's

    print for 1..5

    ;)

    Rob


    Rob Dixon Guest

  12. #11

    Default RE: Counting (easy!)

    On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:
    >> In Perl that is usually written as:
    >>
    >> for $count ( 1 .. 5 ) {
    >> print "$count\n";
    >> }
    > Or even easier:
    > for(1..5) { print; }
    Or _even_ easier;

    print 1..5;

    Hah! :-)


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

  13. #12

    Default Re: Counting (easy!)

    Tore Aursand wrote:
    >
    > On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:
    > >> In Perl that is usually written as:
    > >>
    > >> for $count ( 1 .. 5 ) {
    > >> print "$count\n";
    > >> }
    >
    > > Or even easier:
    > > for(1..5) { print; }
    >
    > Or _even_ easier;
    >
    > print 1..5;

    For the subscribers who don't already know,
    what are the differences between my

    print for 1..5

    and Tore's

    print 1..5

    ? (Deep magic here!)

    Rob


    Rob Dixon Guest

  14. #13

    Default Re: Counting (easy!)

    Rob Dixon wrote:
    >
    > Tore Aursand wrote:
    > >
    > > On Thu, 13 Nov 2003 09:05:45 -0600, Dan Muey wrote:
    > > >> In Perl that is usually written as:
    > > >>
    > > >> for $count ( 1 .. 5 ) {
    > > >> print "$count\n";
    > > >> }
    > >
    > > > Or even easier:
    > > > for(1..5) { print; }
    > >
    > > Or _even_ easier;
    > >
    > > print 1..5;
    >
    > For the subscribers who don't already know,
    > what are the differences between my
    >
    > print for 1..5
    for iterates over the list 1..5 and sets $_ with each value and then
    print is called for each item and print out the value in $_.
    > and Tore's
    >
    > print 1..5
    print is passed the list 1..5 and prints it out.
    > ? (Deep magic here!)
    Not really. :-)



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  15. #14

    Default Re: Counting (easy!)

    On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:
    > Rob Dixon wrote:
    > > For the subscribers who don't already know,
    > > what are the differences between my
    > >
    > > print for 1..5
    >
    > for iterates over the list 1..5 and sets $_ with each value and then
    > print is called for each item and print out the value in $_.
    Not exactly -- it iterates over the *range*, and the range operator
    doesn't need to generate a temporary list here.

    print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
    print for 1 .. $BIGNUM; # creates/discards one scalar at a time

    This is a documented optimization w/r/t foreach() loops, but the same
    thing applies to the foreach() modifier.

    --
    Steve
    Steve Grazzini Guest

  16. #15

    Default Re: Counting (easy!)

    John W. Krahn wrote:
    >
    > >
    > > For the subscribers who don't already know,
    > > what are the differences between my
    > >
    > > print for 1..5
    >
    > for iterates over the list 1..5 and sets $_ with each value and then
    > print is called for each item and print out the value in $_.
    >
    > > and Tore's
    > >
    > > print 1..5
    >
    > print is passed the list 1..5 and prints it out.
    >
    > > ? (Deep magic here!)
    >
    > Not really. :-)
    Hi John.

    I would count you amongst those who already knew, so your
    entry doesn't qualify!

    And no, it's just Perl magic, not Perl Magic :)

    Rob


    Rob Dixon Guest

  17. #16

    Default Re: Counting (easy!)

    Steve Grazzini wrote:
    >
    > On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:
    > > Rob Dixon wrote:
    > > > For the subscribers who don't already know,
    > > > what are the differences between my
    > > >
    > > > print for 1..5
    > >
    > > for iterates over the list 1..5 and sets $_ with each value and then
    > > print is called for each item and print out the value in $_.
    >
    > Not exactly -- it iterates over the *range*, and the range operator
    > doesn't need to generate a temporary list here.
    >
    > print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
    > print for 1 .. $BIGNUM; # creates/discards one scalar at a time
    >
    > This is a documented optimization w/r/t foreach() loops, but the same
    > thing applies to the foreach() modifier.
    It depends on which version of Perl you are using. :-) Older Perls
    would generate the list first and then iterate over it so that using a C
    style for loop would be more efficient.

    When I used the phrase "iterates over the list" I was refering to the
    concept, not the actual implementation. :-)



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  18. #17

    Default Re: Counting (easy!)

    Steve Grazzini wrote:
    >
    > On Thu, Nov 13, 2003 at 12:09:24PM -0800, John W. Krahn wrote:
    > > Rob Dixon wrote:
    > > > For the subscribers who don't already know,
    > > > what are the differences between my
    > > >
    > > > print for 1..5
    > >
    > > for iterates over the list 1..5 and sets $_ with each value and then
    > > print is called for each item and print out the value in $_.
    >
    > Not exactly -- it iterates over the *range*, and the range operator
    > doesn't need to generate a temporary list here.
    >
    > print 1 .. $BIGNUM; # creates list of $BIGNUM scalars
    > print for 1 .. $BIGNUM; # creates/discards one scalar at a time
    >
    > This is a documented optimization w/r/t foreach() loops, but the same
    > thing applies to the foreach() modifier.
    What the code is optimised to is a touch OT, but I've never seen this
    documented Steve. Can you direct me? What do you think is happening
    under the hood for something like

    print for (0..0, 1, 2..5, func(6)..func(99))

    ?

    Rob


    Rob Dixon Guest

  19. #18

    Default Re: Counting (easy!)

    I have a list of email addresses in a text file one to a line. How would I
    seach for a particular email address?

    $email = "myname@aol.com";

    while <FILE> {
    if ($email eq $_) {
    $OK = 1;
    }
    }

    It seems the @ symbol somehow doesn't work in this search. What would be a
    better (or right) way to do this? thanks in advance.


    Jimstone77@aol.com Guest

  20. #19

    Default Re: Counting (easy!)

    [email]dmuey@infiniplex.com[/email] (Dan Muey) wrote in message news:<D6D77DB239A2004BB08A240E2E71460D12C019@LSLOF FICE.infiniplex.infiniplex.com> Or even easier:
    > for(1..5) { print; }
    or even
    print(1..5);
    > Or if you nee the newline:
    > for(1..5) { print "$ \n"; }
    or
    print "$_\n" for 1..5;
    Roy Johnson Guest

  21. #20

    Default Re: Counting (easy!)

    [email]dzhuo@looksmart.net[/email] (David) wrote in message news:<20031113014613.26981.qmail@onion.perl.org>.. .
    > like count down from 5 to 1 slowly in a single row? try:
    >
    > [panda]# perl -e '$|=1; print " @{[6-$_]}\r" and sleep(1) for(1..5)'
    or
    perl -e '$|=1; sleep print "$_\r" for (reverse 1..5)'
    or, if you're feeling evil:
    perl -e 'sleep($|=print "$_\r") for (reverse 1..5)'
    Roy Johnson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139