http://www.perlmonks.org?node_id=930712

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

I just wrote a simple code like this, I want to print $j after $j > 10 and exit. but it doesn't seem to work for me. Can you help me on this?
#! /usr/bin/perl my $i; my $j =1; foo: for ( $i =0; $i<10; $i++ ) { if ( $i < 5 ) { do { $j = $j+2 ; next foo; } until ( $j > 10 ); print $j; last; } }

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: I am a beginner for perl
by toolic (Bishop) on Oct 11, 2011 at 01:06 UTC
Re: I am a beginner for perl
by Khen1950fx (Canon) on Oct 11, 2011 at 02:25 UTC
    What you were trying to do isn't exactly clear to me, but I did this assuming that it was what you wanted. Pay careful attention to toolic's advice.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise; my $i; my $j = 1; FOO: for ( $i = 0 ; $i < 10 ; ++$i ) { if ( $i <= 5 ) { do { $j = $j + 2; }; unless ( $j < 10 ) { print Dumper($j); } } }
Re: I am a beginner for perl
by cavac (Parson) on Oct 11, 2011 at 12:08 UTC
    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.
      hashbang line (first line) shouldn't contain a space between the exclamation mark and the slash
      Not true. Space is allowed.
        Well, yes, it is allowed. Though i'm pretty sure not all programs parsing hashbangs know that.

        But thanks for clarifying that. I should have written it that way in the first place...
        Don't use '#ff0000':
        use Acme::AutoColor; my $redcolor = RED();
        All colors subject to change without notice.