Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: extracting corresponding values from another array

by thinker (Parson)
on Dec 03, 2002 at 11:52 UTC ( [id://217184]=note: print w/replies, xml ) Need Help??


in reply to extracting corresponding values from another array

Hi [Monk},

This should work
#!/usr/bin/perl -w use strict; my @temps = ('69', '70', '71'); my @numbers = ('0.115', '3.667', '4.78'); my $counter=0; my $index=0; my $largest=shift @numbers; for (@numbers){ $index = $counter if ($temps[$counter++]>$largest); } print $temps[$index];

hope this helps

thinker

Update

I'm sure it won't help, as it doesn't work. Sorry.
Here is a tested :-) version.
#!/usr/bin/perl -w use strict; my @temps = ('69', '70', '71'); my @numbers = ('0.115', '3.667', '4.78'); my $index=0; my $largest=$numbers[0]; for (1 .. $#numbers) { if ($numbers[$_] > $largest){ $largest=$numbers[$_]; $index=$_; } } print $temps[$index];

thinker

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

Re: Re: extracting corresponding values from another array
by demerphq (Chancellor) on Dec 03, 2002 at 12:36 UTC
    This is predicated on @numbers being sorted, a precondition we werent provided with, furthermore if the list is empty it returns an incorrect result. Ie 0.

    Sorry, I misread the shift() as somehow being pop(). (/note to self, get glasses checked, and reduce the caffiene intake :-) But its still does return a 0 if there are no elements...

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://217184]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-25 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found