in reply to Convert loop and if constructs from C to Perl

Would a kind monk explain what rules C uses to terminate a block
If there's a block, then the rules are the same as in Perl. But if there's no block, then it's a bit more complicated.

Basically, C expects to see either a statement, or a block. A C statement is either something straightfoward (typically an expression, but it could even be nothing) that ends in a semicolon, or it could be a if or a for control structure, or other similar control structures such those with if/else and while. If those use a block for the body, then they don't require a semicolon.

So, if nested braceless control structures end, they all end in the same place!

I'll not fix your code, but I'll comment it.

01 sub standarization { # was standarization(void) 02 my(i,j,k); 03 for (i=1; i<=10; i++) # where does "for" end? 04 if (d[i][0]!=0) # where does "if" end? 05 for (k=1; k<24; k++) 06 { 07 d[0][k] += d[i][k]; 08 d[i][k] /= d[i][0]; 09 } # end of "for" statement on line 05 # end of "if" on line 04 # end of "for" on line 03 10 11 for (i=9; i<=18; i++) # where does "for" end? 12 if (psb[i][0]!=0) # where does "if" end? 13 for (k=11; k<=BUST; k++) 14 { # new 15 psb[i][k] /= psb[i][0]; 16 } # new # end of "if" on line 12 # end of "for" on line 11 17 18 for (j=1; j<=10; j++) # where does "for" end? 19 if (s[j][0][0]!=0) { 20 for (i=12; i<=18; i++) { 21 if (s[j][i][0]!=0) # where does "if" end? 22 for (k=12; k<=BUST; k++) 23 { # new 24 s[j][i][k] /= s[j][i][0]; 25 } # new # end of "if" on line 21 26 27 if (s[j+10][i][0]!=0) # where does "if" end? 28 for (k=12; k<=BUST; k++) 29 { # new 30 s[j+10][i][k] /= s[j+10][i][0]; 31 } # new # end of "if" on line 27 32 s[j][i][0] /= s[j][0][0]; 33 s[j][i][1] /= s[j][0][0]; 34 s[j+10][i][0] /= s[j][0][0]; 35 s[j+10][i][1] /= s[j][0][0]; 36 } # end "for" line 20 37 38 for (k=17; k<=BUST; k++) { 39 s[j][0][k] /= s[j][0][0]; 40 s[j+10][0][k] /= s[j][0][0]; 41 } 42 } # end of "if" line 19 43 } # end of sub

You'll probably agree by now, that Perl's enforcement of use of braces for control structures, was a sane decision!

Replies are listed 'Best First'.
Re^2: Convert loop and if constructs from C to Perl
by syphilis (Archbishop) on Dec 16, 2006 at 10:33 UTC
    You'll probably agree by now, that Perl's enforcement of use of braces for control structures, was a sane decision!

    That was definitely a sane decision ... but I still find it hard to accept afterthought conditions (aka "modifiers") as valid constructs.

    Yeah ... I know, I know ... it's just me :-)

    (I think in terms of "if condition, do" rather than "do, if condition" .... and nothing's gunna change that :-)

    Cheers,
    Rob
      Maybe penicillin will change your mind? /duck
        Maybe penicillin will change your mind? /duck

        Heh ... DigitalKitty (or was it one of those other spunky perlmonk chicks ?) made a similar remark on the CB a while back.

        As I explained then, I've tried smoking penicillin ... and it didn't help. (Tasted bloody awful, too :-)

        Cheers,
        Rob
Re^2: Convert loop and if constructs from C to Perl
by Anonymous Monk on Dec 16, 2006 at 10:06 UTC
    Got it!

    Thanks so much for explaining the rules along with your code comments.

    Fortunately for me, the other subs I need to convert follow a similar and consistent coding style. I'll certainly apply the rules

    And, I learned a little more about C and a lot about the wisdom behind Perl.

    Best regards, Bernie