in reply to Re: Re: For loop problem
in thread For loop problem
The problem is that the list ($a .. ++$a) is not evaluating a list like you think it is. It is not doing:
japhy -- Perl and Regex Hacker
But rather, it is evaluating its endpoints, and then constructing the list from that. That is the cause of your problem:$start = $a; $end = ++$a; for ($_ = $start; $_ <= $end; $_++) { ... }
You can witness a similar result from:for (++$x .. $x++ . $x) { ... } # starting at ++$x # ending at $x++ . $x # THE END HAS MODIFIED THE START
You see, the arguments to a function aren't copies of the variables, but rather aliases -- by modifying ONE of the $x's, you've modified the others as well.print ++$x, ++$x, ++$x; # 333
Knowing this, the output of this program makes sense to me:
Why is that? Well, the numerator is ++$x, and the denominator is $x--; but in subtracting 1 from $x in the denominator, we have now altered the numerator back to its original state!$x = 3; print ++$x/$x--; # 3/4 => 0.75
I've worked out a solution to your 1 .. 12 loop, then.
See if you can work out why the starting point is 1.for (++$x .. $x++ . $x--) { ... } # or for (++$....$.++.$.--) { ... }
japhy -- Perl and Regex Hacker
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Re: For loop problem
by Arguile (Hermit) on Jun 04, 2001 at 00:26 UTC | |
by chipmunk (Parson) on Jun 04, 2001 at 06:05 UTC | |
by Arguile (Hermit) on Jun 04, 2001 at 06:41 UTC |
In Section
Seekers of Perl Wisdom