If I do
my @answer = qw(java python perl c);
foreach (@answer){
print "[$_]\n";
}
then it will print
[java]
[python]
[perl]
[c]
But, if I add $answer before [$_]:
my @answer = qw(java python perl c);
foreach (@answer){
print "$answer[$_]\n";
}
then it will print
java java java java
Why is that?
Each element in the loop is put into $_. When you say $answer[$_], you're asking for the array element $answer['java'], $answer['python'], and so on. These strings turn into 0 in numeric context, and therefore what you get is $answer[0], which is java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With