[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] That was productive...



Jeff Pinyan writes:
>   $i = 0;
>   for (@array) {
>     # ...
>     $i++;
>   }

The two basic array loops give you either elements or indices.  When I
want both, I modify a for() loop and not a foreach().  The *reason*
the C-style for() loop was invented is that it brings together the
things that should be together: start, stop, and continuation actions.
Your version here hides the key $i++ away at the end of the loop body.

I write:

  for ($i=0; $i < @foo; $i++) {
    local $e = $foo[$i];
    # ...
  }

because I feel it's clearer.  I don't put the localized assignment
into the for() loop clauses, because my eyes are conditioned to read
simple for() loops and I'd have to stop and parse the more complex
form.

TMTOWTDI.  TWTDI will depend a lot on what is pleasing to your eye.

Nat
(not mentioned: efficiency, and the implicit alias in a foreach)

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe