Hi I am new to Perl an in a learning process.
I am having an array
@array = ( 10, 40, 59, 40, 90, 100, 30, 40, 100, 20, );
I want to find the maximum number in the array and also want to know the index where the maximum number is present in my array.
I am doing
my $maxValue = max @array;
print $maxValue; # displays the maximum number in the entire array
my ($index) = grep $array[$_] eq $maxValue , 0.. $#array;
print ($index); # this gives me the index of the maximum number which was found in the array.
The output I am getting is 100 with index 5
But actually 100 is coming 2 times in the array: once at index 6 and again at index 8. My code only provide me the first index it finds with the max value.
How can I get all the index which has maximum value with them?
Seems to be the simplest way.
Although for numbers, you really should use
==, even though e.g. 100 is also a valid string.