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

I have written a small and simple primes generator that just prints out a file that contains a calculated prime number on a new line. But the saving behaviour of this script is rather weird. When I let it run for just some seconds the file is empty. When I wait some more everything is fine and the file seems in order (although it seems to always stop with just 2 digits in the last line). Can anyone explain me why this is happening?

As a side question I would like to ask if there's a better way to check if a number is an int or float than the regexp I used in my script.
open ( SAVE, '>primes.dat' ) or die "Couldn't open primes.dat"; @primes = ( 2 ); $testnumber = 3; while ( 1 ) { foreach $placeholder ( @primes ) { if ( ( $testnumber / $placeholder ) =~ /^[+-]?\d+$/ ) { $testnumber = $testnumber + 2; next; } if ( $placeholder == $primes[-1] ) { push ( @primes, $testnumber ); print SAVE "$testnumber\n"; } next; } }
Thanks

Replies are listed 'Best First'.
Re: Strange saving behaviour
by ChOas (Curate) on Feb 01, 2001 at 19:19 UTC
    Buffering... in a word... buffering...

    add this after you open you file handle:
    select((select(SAVE), $| = 1)[0]);

    Oh, and do look here on Merlyn's site for a really (to me)
    impressive way to calculate primes


    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: Strange saving behaviour
by davorg (Chancellor) on Feb 01, 2001 at 19:20 UTC

    In a word 'buffering'.

    Try putting $|++ near the top of your script and see what happens.

    Or read Dominus's article Suffering From Buffering for a more detailed explaination.

    Update: ChOas's select code works, my $|++ doesn't :(

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      That's what i thought too...

      So I put the $|=1 to force the autoflush,
      BUT it doesn't explain me why the last number is truncated ?

      the last numbers in my file are :
      9899 9901 9907 9917 9923 9929 9931 9941 9949 9959 9967 997

      notes the 3 digit number

        You might want to include an explicit close SAVE; after the loop. Notethis will do no good. we are in an infinite loop here.
Re: Strange saving behaviour
by arturo (Vicar) on Feb 01, 2001 at 19:30 UTC

    Warning: untested as for speed vs. your regex (though it's faster if you put it inline, since it's so simple =), but here's a sub one could use to test for 'integrality':

    sub is_int { my $num = shift; $num == int($num); }

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Hi!

      I should keep my mouth shut, but wouldn't that return 'true'
      for every number above 1 ?..

      I believe 'int' returns the integer part...

      is_int(6.66) would return 6... and not check if it's an int

      Sorry if I'm totally on the wrong track here...

      GreetZ!,
        ChOas

      print "profeth still\n" if /bird|devil/;

        Umm... no. It works as advertised.

        is_int(6.66) returns the result of 6.66 == int(6.66) which is the same as 6.66 == 6 which is false.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

        I think you may have misread it: that's an equality test in the last line, not an assignment =)

        It returns the value of the last statement in the sub, i.e, the value of

        $num == int($num);

        Which is true (== 1) if $num and the integer part of $num are equal to each other, and false (==0)otherwise.

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor