Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:
I was doing something where I needed to have two different increments happening in my "for" loop.
I was doing it like this, because I didn't know any better:
$j=0; for( $i=0; $i<20 ; $i++){ print "\$i = $i and \$j = $j"; $j++ }
but then someone pointed out to me that I could have more than one thing in the three arguments to "for", separated with commas:
for( $i=0,$j=0; $i<20,$j<20 ; $i++,$j++){ print "\$i = $i and \$j = $j"; }
and not only that but I found that "and" and "or" statements worked too:
for( $i=0,$j=0; $i<20 or $j<20 ; $i++,$j++){ print "\$i = $i and \$j = $j"; }
for( $i=0,$j=0; $i<20 and $j<20 ; $i++,$j++){ print "\$i = $i and \$j = $j"; }
OK, just so I can make this into a question, I look up "for" and of course it says "for (EXPR; EXPR; EXPR)", but where's my clue that my EXPR can be a set of statements separated by commas?
I obviously can't separate them with semicolons, but how was I supposed to figure out that commas were the answer?
--
Weaselling out of things is important. It's what separates us from the animals ... except the weasel.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: Newbie realisation about
by Ovid (Cardinal) on Feb 02, 2002 at 00:13 UTC | |
by talexb (Chancellor) on Feb 02, 2002 at 04:58 UTC | |
by Ovid (Cardinal) on Feb 02, 2002 at 08:16 UTC | |
by tilly (Archbishop) on Feb 03, 2002 at 03:43 UTC | |
|
Re: Newbie realisation about
by Zaxo (Archbishop) on Feb 01, 2002 at 23:41 UTC | |
|
Re: Newbie realisation about
by vladb (Vicar) on Feb 02, 2002 at 00:22 UTC |