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....
| [reply] [d/l] [select] |
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 | [reply] |
|
|
| [reply] |
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
| [reply] [d/l] [select] |
|
|
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];
| [reply] [d/l] |
|
|
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....
| [reply] [d/l] |
|
|
|
|
|
|
|
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....
| [reply] |
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 | [reply] [d/l] |
|
|
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....
| [reply] |