in reply to Re^2: Floating point number counting script stuck in a loop
in thread Floating point number counting script stuck in a loop

When you use strict; (highly recommended btw.) you need to declare your variables so you need to stick my in front of the first assignment to $count. However, I'd be inclined to use a C type for loop in this case (note the declaration inside the loop header):

use strict; use warnings; for (my $count = 0.0; $count < 0.9; $count += 0.1) { print "$count\n"; } print "End!\n";

Note too that print doesn't need () and that variables can be interpolated directly into double quoted strings.


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^4: Floating point number counting script stuck in a loop
by Vonunov (Novice) on Oct 25, 2007 at 08:18 UTC
    Huh, I wonder why the tutorial uses (), then? Do they have any purpose or does it just make things look nice, what with all the rounded-off ends? XD

    Thanks for that, by the way, I've always wanted to be able to compress scripts to be painfully tiny. (No sarcasm, it really does look impressive).

    I did read that variables can be put in strings and I had it that way at the beginning, but I thought it might have been the problem and never changed it back after I tested that.

    Thanks for the help.
      Tinyish (but most on perlmonks can do better than I):
      #!/usr/bin/perl -w use strict; for (1..9) { print $_/10, "\n"; }
      Does the same as the question asks - likely wouldn't have accomplished the aim of the exercise, which was probably to teach you about using == with floats, but well ;) See perlvar for explanation of $_
        print "0.$_\n" for 1 .. 9;

        ;)


        Perl is environmentally friendly - it saves trees
        Ha! Nice. Perl golf FTW?

      Then you might like:

      #!/user/bin/perl -w use strict; use warnings; print "@{[$_/10]}\n" for 0 .. 9; print "End!\n";

      Perl is environmentally friendly - it saves trees