I'm not sure what you did, because the for loop I suggested doesn't have a numeric gt() to cause that error. I meant that instead of using the C-style, three-arguments-separated-by-commas for loop, it's almost always better in Perl to do something like this:

for my $i (0..99){ # do something with $i from 0 to 99 } # instead of this: for( my $i=0; $i<100; $i++){ # do something with $i from 0 to 99 }

See how much cleaner the first one is? It also has the advantage of showing you exactly what number it'll start and stop on, so you don't have to remember that $i<100 means it'll stop at 99 (likely to cause off-by-one errors), or use the even uglier $i>=99 as your condition.

There are occasions when the C-style method may make sense, like if you want to step by some number other than 1. They're just very rare. In 15 years of programming in Perl, I'm sure I could count on one hand how many times I've used it, and even those times there was probably a better way.

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.


In reply to Re^4: I have a perl snippet. And I need help understanding it. Can you help answer these questions. by aaron_baugher
in thread I have a perl snippet. And I need help understanding it. Can you help answer these questions. by Fighter2

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.