私はperlを使い慣れていません。私は次のコードの出力を理解できません。perlの '++'演算子の動作
my %fruit_color = (apple => "red", banana => "yellow", grape => "purple");
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
print "The color of apple is ", $fruit_color{"apple"}, "\n";
$cnt = 0;
while ($cnt < @fruits) {
print $fruits[$cnt ++], " ";
}
print "\n";
$cnt = 0;
while ($cnt < @colors) {
print $colors[$cnt ++], " ";
}
出力はここに貼り付けられます:私はこのような私のコードを変更した場合
The color of apple is red
grape banana apple
purple yellow red
しかし、:
my %fruit_color = (apple => "red", banana => "yellow", grape => "purple");
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
print "The color of apple is ", $fruit_color{"apple"}, "\n";
$cnt = 0;
while ($cnt < @fruits) {
print $fruits[$cnt], " "; # DIFF HERE !
$cnt ++;
}
print "\n";
$cnt = 0;
while ($cnt < @colors) {
print $colors[$cnt ++], " ";
}
出力は次のようになります。
The color of apple is red
apple grape banana
red purple yellow
私はこれらの試験の違いを理解することはできません特に最初のwhileループの変更が2番目のループに影響する理由は何ですか?誰かがなぜ出力が逆の順序であるのか教えてもらえますか?どうもありがとう。
については
[印刷する際にハッシュキーが異なる順序を持っていないのはなぜ?](https://stackoverflow.com/questions/30340027/why-do-hash-keys-have-different-order -when-printing) – mkHun
"問題"はあなたの '++'呼び出しではありません。それは完全に大丈夫です。しかし、 'keys%fruit_color;'という呼び出しは、任意の順序でハッシュのキーを返します。異なるスクリプト実行間で同じであることを頼ることはできません。 [docs](http://perldoc.perl.org/functions/keys.html)から:_ "ハッシュエントリは明らかにランダムな順序で返されます。" _ – PerlDuck