こんにちは、私はPerlに慣れ親しんでいます。 私は配列を持っています
@array = ( 10, 40, 59, 40, 90, 100, 30, 40, 100, 20, );
私は配列内の最大数を見つけたいだけでなく、配列内に最大数が存在するインデックスを知りたい。
私がやっている
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.
私が得ている出力は、インデックス5の100です
しかし、実際には配列100の2倍になります.1回はインデックス6に、もう1度はインデックス8になります。私のコードは、最大値で見つかった最初のインデックスのみを提供します。
最大の価値を持つすべてのインデックスを取得するにはどうすればよいですか?
ベストアンサー
my @index = grep $array[$_] eq $maxValue , 0.. $#array;
print @index;
最も簡単な方法と思われます。
数値の場合は、実際には ==
を使用してください。 100も有効な文字列です。