in reply to Re: Re: extracting corresponding values from another array
in thread extracting corresponding values from another array

You might want to make it a bit clearer, less obfuscated:

Likewise. for(;;) is a dirty C trick that doesnt really belong in perl. Especially when the replacement is easier to understand, less prone to error and most importantly more efficient.

y @temps = ('69', '70', '71'); my @numbers = ('0.115', '3.667', '4.78'); my $highNumberIndex = 0; for my $i (0..$#number) { if ($numbers[$highNumberIndex] < $numbers[$i]) { $highNumberIndex = $i; } } print $temps[$highNumberIndex];
And this isnt a valid solution as if the list is empty we get a false return of 0.

--- demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: extracting corresponding values from another array
by Abigail-II (Bishop) on Dec 03, 2002 at 13:09 UTC
    Likewise. for(;;) is a dirty C trick that doesnt really belong in perl. Especially when the replacement is easier to understand, less prone to error and most importantly more efficient.
    Oh, really? A significant part of Perl comes from C, shell, AWK, Ada, BASIC, Python, Fortran, and other languages. Why is for (;;) a dirty C trick that doesn't really belong in Perl, and why isn't for $var (@array) a dirty AWK trick that doesn't belong in Perl? Leave it to the villains of "Harry Potter and the Secret Chamber" to strive for "pure blood".

    I also disagree with your other claims. I'm not convinced that the replacement is easier to understand, less prone to error, and more efficient. Let's consider the "dirty C trick":

    for (my $i = 0; $i < 1000000; $i += 2) {$sum += $i}

    The AWK-like replacement would be:

    for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i}

    I don't think that's easier to understand, or less prone to error. As for efficientcy:

    #!/usr/bin/perl use strict; use warnings; use Benchmark; timethese -10, { C => 'my $sum; for (my $i = 0; $i < 1000000; $i += 2) {$sum += + $i}', AWK => 'my $sum; for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum + += $i}' }; __END__ Benchmark: running C, AWK for at least 10 CPU seconds... C: 10 wallclock secs (10.66 usr + 0.00 sys = 10.66 CPU) @ 1.41/ +s (n=15) AWK: 10 wallclock secs (10.08 usr + 0.00 sys = 10.08 CPU) @ 0.69/ +s (n=7)

    Abigail

      Abigail,

      That was an off the cuff remark about using for(;;) instead of for ($lower..$upper). Of course there are uses of for(;;) that do not map on to for ($i..$j). However this wasnt one of them. And for this type of case I stand by my remarks.

      Furthermore comparing

      for (my $i = 0; $i < 1000000; $i += 2) {$sum += $i}
      to
      for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i}
      is a bit strange, when internally it is closer to
      { my $i=0; while ($i < 1000000) { $sum+=$i; } continue { $i+=2; } }
      As for all the other languages you listed, none (that I know of) but the C derivatives have this weird form of while loop pretending to be a for loop. For a programmer whose experience is limited to one of the ones without the equivelence the construct is particularly confusing. So from that POV I stand by calling the three arg for loop construct a dirty C trick.

      And I stand by the efficiency claims for the case to which I was primarily referring:

      use strict; use Benchmark qw(cmpthese); our $sum; cmpthese -10,{ 'foreach' => <<'CODE', 'for(;;)' => <<'MORECODE' }; $::sum=0; for my $i (0..10_000) { $::sum+=$i; } CODE $::sum=0; for (my $i=0;$i<10_000;$i++) { $::sum+=$i; } MORECODE __END__ Benchmark: running for(;;), foreach, each for at least 10 CPU seconds. +.. for(;;): 11 wallclock secs (10.61 usr + 0.00 sys = 10.61 CPU) @ 85 +.30/s (n=905) foreach: 11 wallclock secs (10.63 usr + 0.00 sys = 10.63 CPU) @ 14 +3.15/s (n=1521) Rate for(;;) foreach for(;;) 85.3/s -- -40% foreach 143/s 68% --
      Id say thats more efficient wouldnt you?

      --- demerphq
      my friends call me, usually because I'm late....

        Here's the dirty C trick in Java:
        public class Loop { public static void main (String [] argv) { for (int i = 0; i < 10; i ++) { System . out . println (i); } } }

        And here it is in AWK:

        BEGIN {for (i = 0; i < 10; i ++) {print i}}

        And here it is in LPC:

        void loop () { int i; for (i = 0; i < 10; i ++) { write (i + "\n"); } }

        It's not just C.

        Feel free to find for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i} a bit strange, because it comes closer to the quoted while statement. But then, don't say that when we change the $i += 2 to $i ++ the alternative is a foreach. That would also be closer to the while.

        I know the foreach is more efficient than the for, but only in the case of incrementing the variant by 1. The for() is far more flexible than the foreach(). The for() is a less error-prone, and easier way of writing the while.

        Abigail