in reply to RE: Buzzcutbuddha (C Style For and Foreach The Same): Finding the length of an array of arrays
in thread Finding the length of an array of arrays

No correction in fact. I knew they were aliases all along. But I find it clearer to indicate my thoughts by saying "for" for the classic C loop and "foreach" for iterating over a list. Particularly when I am trying to tell C programmers why they should use foreach style loops rather than for loops. ;-)
  • Comment on RE (tilly) 2: C Style For and Foreach The Same

Replies are listed 'Best First'.
RE: Buzzcutbuddha(You provided disinformation): C Style For and Foreach The Same
by buzzcutbuddha (Chaplain) on Aug 18, 2000 at 17:34 UTC
    I was correcting you because you said that foreach is faster, etc. I think it would have been better to say that foreach is Perl Style and easier to understand, and better to use for those reasons...

    just my $0.02
      Read what I wrote exactly as I wrote it the first time.

      I said that foreach loops are faster than classic C-style loops. Yes, you can write a classic C-style loop using foreach. But nobody thinks that way.

      And indeed iterating directly over a list is *significantly* faster, less error prone, etc, than doing an explicit loop over the indices, looking up array values inside the loop. That is not disinformation, that is simple fact and I have found that that factual information carries a lot more weight with C programmers starting out with Perl than saying, "Well the standard Perl idiom is..."

      Have you ever said, "This is Perl Style and easier to understand." and been told, "Well I am a C programmer, I have been using for loops for 15 years and they are easier for me to understand because I am used to them."? You won't win that argument...

        I stand corrected. You are correct on your points. Sorry for the accusation. For those interested, here is the code and results from benchmarking.
        code:
        use Benchmark; sub foreach1000() { my @x = (1..1000); foreach(@x) { my $z = $_; } } sub for1000() { my @x = (1..1000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } sub foreach10000() { my @x = (1..10000); foreach(@x) { my $z = $_; } } sub for10000() { my @x = (1..10000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } timethese( 10000, { for1000 => 'for1000()', foreach1000 => 'foreach100 +0()', for10000 => 'for10000()', foreach10000 => 'foreach10000()' });
        results:
        Benchmark: timing 10000 iterations of for1000, for10000, foreach1000, +foreach10000... for1000: 59 wallclock secs (53.75 usr + 0.03 sys = 53.78 CPU) @ 18 +5.95/s (n=10000) for10000: 676 wallclock secs (537.40 usr + 0.23 sys = 537.63 CPU) @ + 18.60/s (n=10000) foreach1000: 33 wallclock secs (24.67 usr + 0.00 sys = 24.67 CPU) @ 4 +05.27/s (n=10000) foreach10000: 293 wallclock secs (250.74 usr + 0.21 sys = 250.95 CPU) + @ 39.85/s (n=10000)
RE (\d+): C Style For and Foreach The Same
by turnstep (Parson) on Aug 18, 2000 at 17:31 UTC

    Why confuse them though? I would think that telling them up front that they are the same would be easier for them in the long run. Bad enough that some perl programmers still think that they are somehow different. I never use "foreach" myself, but that's because I am exercising the first Perl Virtue. :)

      What makes you think I confuse them?

      I show them the idiom as I write it and encourage them to write it the same way. If the question of whether they are the same comes up, I just say, "Yeah, they are aliases for each other, but I like to write them differently because I think it is clearer that way." I have yet to see anyone find that a huge hurdle. I have seen getting used to the different type of looping be a hurdle.

      As for telling people things up front, do you think that up front I tell people that blocks are really hashes, REs are really done by recursion, or many other minor details of the language? Perl is a big language, with many corners, and you shouldn't present it all at once.