R
Raymundo
Hello,
I'm sorry I'm not good at English.
foreach and map functions show the same result when an array has no
gap.
@array = (1, 2, 3, 4);
foreach (@array) {
$_ *= 10
}
# now, $array = (10, 20, 30, 40)
@array = (1, 2, 3, 4);
map { $_ *= 10 } @array;
# now, $array = (10, 20, 30, 40)
However, if an array contains a gap...
1 $array1[0] = 0;
2 $array1[9] = 9; # now $array1 = (0, undef,
undef, ... , 9);
3 print "@array1", "\n"; # 0 "" "" ... "" 9
4 foreach (@array1) {
5 $_ *= 10 # $array1 = (0, 0, 0, ... , 90)
6 }
7 print "@array1", "\n"; # 0 0 0 ... 0 90
8
9
10 $array2[0] = 0;
11 $array2[9] = 9;
12 print "@array2", "\n"; # 0 "" "" ... "" 9
13 map { $_ *= 10 } @array2; # ERROR!!!!!!
14 print "@array2", "\n";
line 1-7 work well, but using map, line 13 reports an error:
Modification of a read-only value attempted at t2.pl line 13.
Before line 13, line 12 prints the intervening elements, treating
undef as null string. Then why does line 13 make such error? Is it a
bug? or...?
I'm sorry I'm not good at English.
foreach and map functions show the same result when an array has no
gap.
@array = (1, 2, 3, 4);
foreach (@array) {
$_ *= 10
}
# now, $array = (10, 20, 30, 40)
@array = (1, 2, 3, 4);
map { $_ *= 10 } @array;
# now, $array = (10, 20, 30, 40)
However, if an array contains a gap...
1 $array1[0] = 0;
2 $array1[9] = 9; # now $array1 = (0, undef,
undef, ... , 9);
3 print "@array1", "\n"; # 0 "" "" ... "" 9
4 foreach (@array1) {
5 $_ *= 10 # $array1 = (0, 0, 0, ... , 90)
6 }
7 print "@array1", "\n"; # 0 0 0 ... 0 90
8
9
10 $array2[0] = 0;
11 $array2[9] = 9;
12 print "@array2", "\n"; # 0 "" "" ... "" 9
13 map { $_ *= 10 } @array2; # ERROR!!!!!!
14 print "@array2", "\n";
line 1-7 work well, but using map, line 13 reports an error:
Modification of a read-only value attempted at t2.pl line 13.
Before line 13, line 12 prints the intervening elements, treating
undef as null string. Then why does line 13 make such error? Is it a
bug? or...?