in reply to Re^3: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
in thread I have a perl snippet. And I need help understanding it. Can you help answer these questions.

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.

  • Comment on Re^4: I have a perl snippet. And I need help understanding it. Can you help answer these questions.
  • Select or Download Code