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

for($i=2; $a <= 7; $a++) { if($slog[$i]) { $a = $i + 5; print "$slog[$i] $slog[$a]"; } }
I dont get this, I have more than one, there info in $slog3 its just printing $slog2 and $slog7

Replies are listed 'Best First'.
Re: for only returning one result
by chromatic (Archbishop) on Jul 21, 2002 at 17:11 UTC

    Well yeah, that's exactly what your loop does!

    I don't know what $a starts as, but I'm assuming 0. On the first hit, $i is 2. $a is less than seven. If $slog[2] is true, $a is set to 2 + 5, or 7. Then $slog[2] and $slog[7] are printed. At the end of the loop, $a is incremented to 8. On the next loop, $a is 8 and the condition is false, so the loop stops.

Re: for only returning one result
by Chady (Priest) on Jul 21, 2002 at 14:53 UTC

    hmm.. strange use of for.. is $a initialized somewhere else? $i never changes, and if if ($slog[$i]) returns true it will always return true.. whatever happens to $a, and that's probably not the effect you want.. perhapps a little bit more code might help us help you.

    Oh, and you might want to change that for loop into the more perlish for $a ( 2..7 ) { kinda thing.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
•Re: for only returning one result
by merlyn (Sage) on Jul 22, 2002 at 15:46 UTC