Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

This is a minor thing, that I bet all the senior monks know, but I only just found it out, and it was very useful, so maybe newbies like me might be interested.

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

    Also, most Perl programmers shy away from the "C-style" for loop. This is the C style:

    for( $i=0; $i<20 ; $i++){ print "\$i = $i\n"; }

    Whereas in idiomatic Perl, you'll typicall see things like the following:

    for ( 0 .. 19 ) { print "$_\n"; }

    The ".." is referred to as the range operator. It's faster than the C-style for loop and, once you get used to it, it's easier to read. See the "Range Operator" section in perlop.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Whereas in idiomatic Perl, you'll typicall see things like the following:
      for ( 0 .. 19 ) { print "$_\n"; }

      ... with the reminder that ( 0 .. 1000000 ) would put an undue strain on your machine's memory as it generates all million (plus one) numbers in a list. And if you have two nested loops, you have to do the 'hot potatoe' thing with $_ so that its value doesn't get overwritten.

      for ( 0..7 ) { my $Col = $_; # Eek! Save $_ otherwise this value is lost for ( 0..7 ) { # Do something in an 8x8 matrix. if ( $Matrix[ $Col ][ $_ ] == 4 ) ... } }
      It's a matter of preference .. I would rather just write
      for ( my $Col = 0; $Col < 8; $Col++ ) { for ( my $Row = 0; $Row < 8; $Row++ ) { # Do something in an 8x8 matrix. if ( $Matrix[ $Col ][ $Row ] == 4 ) ... } }
      But that's my years of C programming colouring my view of Perl.

      --t. alex

      "Of course, you realize that this means war." -- Bugs Bunny.

        talexb wrote:

        ... with the reminder that ( 0 .. 1000000 ) would put an undue strain on your machine's memory as it generates all million (plus one) numbers in a list.

        That's no longer true for recent versions of Perl. From perldoc perlop (perl -v 5.6.1):

        The range operator is useful for writing "foreach (1..10)" loops and for doing slice operations on arrays. In the current implementation, no temporary array is created when the range operator is used as the expression in "foreach" loops, but older versions of Perl might burn a lot of memory when you write something like this:
        for (1 .. 1_000_000) { # code }

        talexb also wrote:

        And if you have two nested loops, you have to do the 'hot potatoe' thing with $_ so that its value doesn't get overwritten.

        Also not accurate. In that case, simply name the resulting value!

        for my $first ( 0..7 ) { for my $second ( 0..7 ) { # Do something in an 8x8 matrix. if ( $Matrix[ $first ][ $second ] == 4 ) ... } }

        Idiomatic Perl to the rescue again :)

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        In addition to the points made by Ovid, Perl-style foreach loops are a lot less error prone than C-style for loops are.

        People who use C-style loops, whether it be in C or another language, typically have off-by-one errors as their most common type of bug. By simply switching to Perl-style loops when programming in Perl, you kill the opportunity for this mistake, and therefore will have fewer bugs to track down.

        I have seen this happen in enough cases that I can't accept the argument that it is simply personal preference. When you have a choice, use the Perlish looping mechanism. The time saved in avoiding debugging will quickly make this pay for itself.

Re: Newbie realisation about
by Zaxo (Archbishop) on Feb 01, 2002 at 23:41 UTC

    perlop, see the comma operator. It is very useful for sticking together several EXPRs in sequence and retaining an EXPR.

    After Compline,
    Zaxo

Re: Newbie realisation about
by vladb (Vicar) on Feb 02, 2002 at 00:22 UTC
    Yes, in perldoc perlop it states that:
    Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator.
    So, when you do something like for($i=0,$j=0,...; ...) {...} the comma operator will cause the list of smaller 'expressions' (terminated by the first occurance of the ';' ) execute first.

    As far as the second EXPR goes, it has to be a boolean expression. I nomrally use '||' and '&&' as they return value of the last expression evaluated. For example, '&&' would return value of expression to the right if one to the left is true. With regard to 'and'/'or' operands, they are similar with the exception that they bear lower precedence. Therefore, they are best used for control flow rather than in 'boolean' type operations.

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith