in reply to Re: how to traverse thru this hash?
in thread how to traverse thru this hash?

Is there any benefit to using "for" instead of "foreach", or is it just a style thing?

Replies are listed 'Best First'.
Re^3: how to traverse thru this hash?
by japhy (Canon) on Nov 04, 2005 at 00:55 UTC
    My fingers are saved some work? Honestly, 'for' and 'foreach' are interchangeable in Perl syntax. Just read perlsyn.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Thanks for that link. Interestingly enough, at the bottom of the foreach section in that document, this is written:

      And it's faster because Perl executes a foreach statement more rapidly than it would the equivalent for loop.

      So you might want to consider typing those four extra letters. :)
        Context, context, context.
        The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity.
        and then later:
        And it's faster because Perl executes a foreach statement more rapidly than it would the equivalent for loop.
        That second quote (yours) is in regards to for ($i = 0; $i < @array; $i++) { $array[$i] ... } vs. foreach (@array) { $_ ... }

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart