Perl's if statements and loop BLOCKS are bounded by curly braces. There is no ambiguity when using compound constructs and nested loops.

While attempting to convert the C code below, I was able to deduce where to block 3 loops with "curlies" as indicated by # new.

However, there are 7 places where the C program has an if statement or for loop with no obvious (to me) end. Indenting may suggest a block termination but this is only conjecture on my part and I cannot be certain the indenting is correct.

There are several C routines I want to convert with similar structures. Would a kind monk explain what rules C uses to terminate a block and then correct the code to show where the curly braces should go to make it work with Perl.

Thanks

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 } 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 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 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 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

In reply to Convert loop and if constructs from C to Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.