Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, My problem is that I need a way of finding the corresponding value of a number in one array, to that in another array. e.g. I have an array called @temps, and an array called @numbers. I am finding the highest number from @numbers but need to know the value corresponding to it in @temps.
my @temps = ('69', '70', '71'); my @numbers = ('0.115', '3.667', '4.78');
e.g. if my program returns the highest number in the array as '4.78', how can i get it to return '71' aswell???????? thanks ;-)

Replies are listed 'Best First'.
Re: extracting corresponding values from another array
by demerphq (Chancellor) on Dec 03, 2002 at 12:29 UTC
    my @temps = (69, 70, 71); my @numbers = (0.115, 3.667, 4.78); my ($max,$index)=(0); $numbers[$_]>$max and (($max=$numbers[$_]),($index=$_)) foreach 0..$#numbers; if (defined $index) { print "Index:$index Max:$max Corresponding:$temps[$index]"; } else { print "No elements found that are larger than 0"; }
    Outputs
    Index:2 Max:4.78 Corresponding:71
    HTH

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

Re: extracting corresponding values from another array
by claree0 (Hermit) on Dec 03, 2002 at 11:49 UTC
    I'd consider using a hash rather than two arrays.

    Clare

      If the question is about Perl and the answer is not either "use a hash" or "use a module that uses a hash", the answer is probably wrong.

      --
      Regards,
      Helgi Briem
      helgi AT decode DOT is

Re: extracting corresponding values from another array
by thinker (Parson) on Dec 03, 2002 at 11:52 UTC
    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
      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....

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

Re: extracting corresponding values from another array
by pike (Monk) on Dec 03, 2002 at 13:30 UTC
    From what I recall, it is ineffective to access list elements by index, if you have to check all elements - at least this is true for large lists. Therefore, the following might be an alternative:

    my (@temps, @numbers); my ($ind, $maxind, $max) = (-1, -1, $num[0]); for my $num (@numbers) { $ind++; $maxind = $ind, $max = $num if $num > $max; } my $maxtemp = $temps[$maxind];

    pike

      From what I recall, it is ineffective to access list elements by index

      Any chance you might throw together a benchmark so we can see?

      Also, while your solution does distinguish between an empty list and the highest being at 0, it'll die on the last line if the array is empty.

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