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

Hi fellow Monks!
So,here is the deal...
I am taking a course and I am supposed to write small perl scripts, BUT, it seems that i have forgotten the basics :)
For example, if it asks:
Use a loop to write all numbers between 1 and 10

I could do it by simply
for ($j=1; $j<=10; $j++) { print "$j\n"; }
, but, is there a way of doing it with a while loop?

Another one:
Calculate the factorial for a user-defined number.

again here I could use Module from Perl, but the idea is to write it again by using while loop.
Can you give me some suggestions?

Replies are listed 'Best First'.
Re: SIMPLE way to write these?
by choroba (Cardinal) on Feb 04, 2014 at 14:09 UTC
    for also introduces a loop. The canonical way to print the numbers between 1 and 10 would be
    for my $i (1 .. 10) { print "$i\n"; }

    Or, even, something like

    use feature qw{ say }; say for 1 .. 10;

    With a while loop, you just have to initialize a counter, and then check its value against the final value in the loop condition:

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

    The factorial is left as an exercise for the reader.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Aham, I see, but how can you use a while loop to print all numbers between $number1 and $number2 ( I mean if they are both user-defined)?
        Ahem, just replace 1 (that is not followed by 0) with $number1; and similarly, replace 10 with $number2.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: SIMPLE way to write these?
by tobyink (Canon) on Feb 04, 2014 at 15:20 UTC

    Any C-style for loop:

    for (INIT; COND; POST) { BODY; }

    Can always be rewritten as a while loop like this:

    INIT; while (COND) { BODY; POST; }

    So your loop can be written as:

    $j=1; while ($j<=10) { print "$j\n"; $j++; }

    However, in Perl there are more idiomatic ways of writing it. For example:

    print "$_\n" for 1..10;

    Or, if you've got a recent version of Perl, and enable the "say" feature:

    say for 1..10;
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: SIMPLE way to write these?
by johngg (Canon) on Feb 04, 2014 at 16:03 UTC

    It is also worth mentioning that while can be used as a statement modifier.

    $ perl -Mstrict -Mwarnings -E ' my $i = 1; say +( $i ++ ) while $i <= 10;' 1 2 3 4 5 6 7 8 9 10 $ perl -Mstrict -Mwarnings -E ' my $i = 1; say do { $i ++ } while $i <= 10;' 1 2 3 4 5 6 7 8 9 10 $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: SIMPLE way to write these?
by karlgoethebier (Abbot) on Feb 04, 2014 at 14:39 UTC

    In the only lecture i had about algorithms the professor told us: "Shure, you can cheat but then you need to explain it to me..." ;-)

    #!/usr/bin/perl use strict; use warnings; my ( $c, $f ) = ( 10, 1 ); $f *= $c-- while $c > 0 ; print $f; __END__ karl@host ~ $ ./factorial.pl 3628800

    Wolfram Alpha

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: SIMPLE way to write these?
by 2teez (Vicar) on Feb 04, 2014 at 14:27 UTC

    ..Use a loop to write all numbers between 1 and 10.. Using the C for loop like you did, included 1 and 10. I thought the question says numbers BETWEEN?
    If they are included, well and good but if not, you might have to write for (my $j = 2; $j < 10; $j++){...} OR better  print $_,$/ for 2 .. 9

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: SIMPLE way to write these?
by pvaldes (Chaplain) on Feb 04, 2014 at 15:18 UTC

    foreach (1..10){print "$_\n"}

    or, with while:

    my $i = 0; while ($i < 11){++$i; print $i};

    as Choroba said before