http://qs1969.pair.com?node_id=1041507

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

Good afternoon perl monks. I have this question and I have come up with the code below so far but I cant get it to work.

This is the question: Write a Perl program that prints out all the even numbers from 0 to 20. Print this list 4 times, using while, until, do-until and for loops (so you need to use all 4 loop structures!

use strict use warnings my $i = 0; while (0); do {} until 0; for (0..0) {}; for (1..4) { print 2*$_, "\n", foreach (1..20);

Replies are listed 'Best First'.
Re: Perl Loops
by rjt (Curate) on Jun 30, 2013 at 01:25 UTC
    I have come up with the code below so far but I cant get it to work.
    use strict use warnings

    Clearly this isn't your actual code, as these statements are missing semicolons and would generate a syntax error. Please post actual code in the future, as it avoids a whole mess of problems like this, and often helps you answer your own question!

    my $i = 0; while (0); do {} until 0; for (0..0) {};

    It looks like you've managed to post something that looks like code you've tried, which is a start, but I doubt you've tried very hard to make this work, as your loops all very clearly do nothing. If you really are very stuck on how to write any sort of a loop, your course notes probably contain examples, plus the plethora of Perl beginner's material out there contains plenty:

    This is the question: Write a Perl program that prints out all the even numbers from 0 to 20. Print this list 4 times, using while, until, do-until and for loops (so you need to use all 4 loop structures!

    This looks like homework, and there's nothing wrong with that per se, but I would strongly encourage you to read some of the links I've provided and try to work out the basics for yourself, or this exercise will be little more than a colossal waste of your time, and, hey, you're worth it!

Re: Perl Loops
by davido (Cardinal) on Jun 29, 2013 at 22:04 UTC

    Presumably you've gotten it all working now. But I just wanted to mention something that I hope will aid you as you start into learning to program.

    Always look at the error messages you're getting. Always start with the first one and figure out why you're getting it. After you've fixed it, move on to the next.

    In the case of your code, first you'll get a syntax error on line seven, because there's no code block following the while(0) syntax.

    The next syntax error will be your final for loop, which isn't terminated with a closing brace.

    Your final foreach loop works as a statement modifier, so the comma between the "\n" and the foreach is unnecessary, and confusing.

    If you get in the habit of looking at the error messages with an attitude of, "I should figure out what this is telling me, and why.", you'll find your emerging adventure in programming more fun.


    Dave

      Very helpful Dave!thanks
Re: Perl Loops
by farang (Chaplain) on Jun 29, 2013 at 21:04 UTC

    Good that you are using strict and warnings, but you have to end the expressions with a semicolon.

    use strict; use warnings; ...
    Doing this will allow you to get information on problems in your code. You have a syntax error for the while line and a missing curly brace in the last line.

    I'm fairly sure you are missing the purpose of this assignment, though. The only way it makes sense to me is for you to write four separate pieces of logic, each printing the numbers successfully. The only code you have close to doing this so far is part of one line.

    print 2*$_, "\n", foreach (1..20);
    This could be rewritten if you want as follows, but note you are still missing zero in the printout.
    for ( 1..20 ) { print 2*$_, "\n"; }

    I suggest you read the documentation carefully for while, until, for, and do in perlsyn and try some examples from there to see how you can write similar code which does what you want. Just having monks here give you working code will not have long-term value for you, but learning how to read documentation and produce working code yourself will be of great value.

      Thank you! You were right. I though U was supposed to write one code but from your explanation I have to write four codes. It makes more sense since I also have to print out four times. Thank you!
Re: Perl Loops
by vinoth.ree (Monsignor) on Jun 29, 2013 at 20:34 UTC

    Hi stam1982

    For:
    #!/usr/bin/perl use strict; use warnings; for (my $n = 2; $n < 21; $n += 2){ print "$n\n"; }
    while:
    #!/usr/bin/perl use strict; use warnings; my @evens; my $n = 0; my $number_to_give_up_at = 21; # $#evens will contain value of index of last element in @evens array while ($n++ < $number_to_give_up_at and $#evens < 9){ push @evens, $n if $n % 2 == 0; } print join("\n", @evens);

    All is well
      Farang: How will you do that using the until/do-until loops?