in reply to Re: Search Engine Output needs sorting
in thread Search Engine Output needs sorting

Wait! Is that true for all for loops in Perl? You can never use the C-style method? I always have to use foreach or that .. operator instead?

  • Comment on Re^2: Search Engine Output needs sorting

Replies are listed 'Best First'.
Re^3: Search Engine Output needs sorting
by MidLifeXis (Monsignor) on May 31, 2011 at 13:28 UTC

    You can never use the C-style method?

    I would not say never use -- there are instances where it might make better sense*. However, for simple (i=0;i<max;i++)-type loops, Perl has a different method of specifying it: for my $i (0..$max-1).

    This is a question of style and understanding. Just as not knowing idiomatic expressions (or using them incorrectly) can tag someone as "not from 'round here", using the C-style for loop in the general case may cause some to question how well you know Perl -- right or wrong.

    On the other hand (and this may be heresy), if you are in a C shop, and your coding standards are written from a C point of view, I suppose that could be an argument for using the C-style for loop. Not necessarily a good argument, but an argument.

    * - perhaps a time that you would want to use a C-style loop is if the increment is something other than 1, or if the exit test is something other than a simple comparison. However, this could be possibly written better in a different construct (while, for example).

    --MidLifeXis