in reply to Re^3: SIMPLE way to write these?
in thread SIMPLE way to write these?

Like this?
my $i = $number1; while ($number2 >= $number1) { print $i++, "\n"; }
It goes into an infinite loop...I am clearly making a silly mistake here...

Replies are listed 'Best First'.
Re^5: SIMPLE way to write these?
by GotToBTru (Prior) on Feb 04, 2014 at 15:04 UTC

    You are comparing $number2 and $number1 in your while; neither ever changes. That's why you get an infinite loop. Here is a solution that preserves the value in $number1.

    my $number1 = 1; my $number2 = 10; my $i = $number1; while ($number2 >= $i) { print $i++,"\n"; }

      I think most would write this using the three argument for() statement:

      my $number1 = 1; my $number2 = 10; for (my $i = $number1; $i <= $number2; $i++) { print "$i\n"; }

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.