for loop in perl

前端 未结 4 1634
灰色年华
灰色年华 2021-01-22 15:35
my @array=(1..10);
for my $i (@array){$i++;}
print \"array is now:@array\";

this is changing the values of the array. Why?

4条回答
  •  心在旅途
    2021-01-22 16:05

    I believe it is because in perl when you loop through an array, each element is passed by reference, meaning when you change $i in the loop, it is changing the actual value in the array. I'm not sure how to make it pass by value, though.

提交回复
热议问题