You have a logic problem. The next prevents you from evaluating the until, which in turn prevents you from reaching the print.
What are you really trying to do? | [reply] [Watch: Dir/Any] [d/l] [select] |
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);
}
}
}
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
hashbang line (first line) shouldn't contain a space between the exclamation mark and the slash
Not true. Space is allowed.
| [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |