ellem has asked for the wisdom of the Perl Monks concerning the following question:

Considering this code:
#! /usr/bin/perl use warnings ; use strict ; use Time::localtime ; my $start = ctime() ; my $one = 1 ; until ($one == 100001) { print $one++ ; print "\n" ; } my $end = ctime() ; print "Started $start\n Ended $end\n\n" ;
How could I get the output of
print $one++
to print say every 500th addition?
--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
Re: Printing Some Output
by Limbic~Region (Chancellor) on Aug 01, 2003 at 15:45 UTC
    ellem,
    I would use the modulus (%) operator.
    until ($one == 100001) { $one++; print "$one\n" if ($one % 500 == 0); }

    Cheers - L~R

Re: Printing Some Output
by fletcher_the_dog (Friar) on Aug 01, 2003 at 16:36 UTC
    #! /usr/bin/perl use warnings ; use strict ; use Time::localtime ; my $start = ctime() ; my $one = 1 ; until ($one == 100001) { print $one."\n" if not ++$one % 500; } my $end = ctime() ; print "Started $start\n Ended $end\n\n" ;
Re: Printing Some Output
by blue_cowdawg (Monsignor) on Aug 01, 2003 at 15:47 UTC

    I guess you haven't heard of modulo arithmetic... :-) Try this:


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

      blue_cowdawg,
      There are two slight problems with that. The first is that the increment will never happen since the if is never true - infinite loop. The second is the print "\n" after will cause the screen to go blank quick.

      Cheers - L~R

        I thought of that belatedly, but was hoping the OP would figure it out on their own...

        Besides... see the disclaimer in my .sig... :-)


        Peter @ Berghold . Net

        Sieze the cow! Bite the day!

        Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

        Brewer of Belgian style Ales

Re: Printing Some Output
by Abigail-II (Bishop) on Aug 01, 2003 at 15:57 UTC
    my $one = 0; while ($one <= 100_000) { print $one += 500, "\n"; }

    Abigail

      Very funny, but not entirely what the OP had in mind I might guess.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Abigail,
      One minor nit - ellem asked "to print say every 500th addition?". Your solutions prints every addition - it just adds 500 each time.

      Cheers - L~R

      Update: CountZero beat me to the punch :-(

Re: Printing Some Output
by NetWallah (Canon) on Aug 01, 2003 at 20:13 UTC
    In the interest of readable code, I would much prefer:
    #! /usr/bin/perl use warnings ; use strict ; use Time::localtime ; my $start = ctime() ; for (my $one=1; $one<100001; $one++){ print $one."\n" if not $one % 500; } my $end = ctime() ; print "Started $start\n Ended $end\n\n" ;