First of all, the hashbang line (first line) shouldn't contain a space between the exclamation mark and the slash, so instead of
#! /usr/bin/perl
it should be
#!/usr/bin/perl

Second, i have the feeling that your code is overly complicated. If you loop $i 10 times and only use the first five times, why loop the 10 times anyway? do/until isn't that readable, if or while could do a better job. The foo: tag and next jump also don't help to clarify the code.

There also seems to be another logical flaw with foo/next/do/until: I'm just not sure what result you expect: Just increment $j until it's greater than 10?.

Ok, given above assumptions, let's try to write an easier version, shall we?

#!/usr/bin/perl use strict; use warnings; my $i = 0; my $j = 1; while($i < 10) { if($i < 5) { $j += 2; } if($j > 10) { last; } } print "Result: $j\n";


If my assumptions are correct so far, we could just remove variable $i altogether and just increment $j until the result is reached:
#!/usr/bin/perl use strict; use warnings; my $j = 1; while($j <= 10) { $j += 2; } print "Result: $j\n";


Of course, in this simple example, we could also provide a formula to do the same. In this case, instead of hardcoded numbers 10 and 2 i'll use $minimum and $stepsize:
#!/usr/bin/perl use strict; use warnings; my $j = 1; my $minimum = 10; my $stepsize = 2; my $steps = int($minimum/$stepsize); # If startpoint is zero, we need to add a step if($j == 0) { $steps++; } $j = ($steps * $stepsize) + $j; print "Result: $j\n";


While the last solution looks more complicated, it scales better. Think is you don't have 10 steps but 10 billion. The formula would still take the same time while the loop constructs will run for a long time.

Hope that helped to clarify a bit and i didn't misunderstand your problem.
Don't use '#ff0000':
use Acme::AutoColor; my $redcolor = RED();
All colors subject to change without notice.

In reply to Re: I am a beginner for perl by cavac
in thread I am a beginner for perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.