in reply to 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:
my @temps = ('69', '70', '71'); my @numbers = ('0.115', '3.667', '4.78'); my $highNumberIndex = 0; for (my $i = 0; $i < @number; $i++) { if ($numbers[$highNumberIndex] < $numbers[$i]) { $highNumberIndex = $i; } } print $temps[$highNumberIndex];

Replies are listed 'Best First'.
Re: Re: Re: extracting corresponding values from another array
by demerphq (Chancellor) on Dec 03, 2002 at 12:34 UTC
    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....

      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....