perl - getting index of max value of array
shwaip
bluffin' with my muffin Icrontian
I'm going to have an array of floats, and I need to get the index of the max value. I know I can just iterate through the array and find the largest number, and save its index, but...is there a builtin perl function that does this?
0
Comments
[php]
findMaxValueIndex(@array) {
for ($i = 0, $index = 0, $max = 0; $i < scalar(@array); $i++) {
if ($array[$i] > $max) {
$max = $array[$i];
$index = $i;
}
}
return $index;
}
[/php]
My Perl is very rusty so my @array usage may be wrong.
[php]
$ind = 0;
$max=$array[0];
$cnt = 0;
for my $val (@array) {
if ($val > $max) {
$max = $val;
$ind = $cnt;
}
$cnt++;
}
[/php]
It wasn't something really complicated, so I didn't worry too much about writing my own code.