B
bill
Something like this burned me really bad once:
my @x = (1) x 3;
print "@x\n";
for my $y (@x) {
$y = 0;
}
print "@x\n";
__END__
1 1 1
0 0 0
It's as if the loop variable $y was a reference to the current
element of the array, except that it doesn't need to be dererenced.
Surprisingly, to me at least, this doesn't produce any errors:
for my $y (1..3) {
$y = 0;
}
Perl doesn't see the above as analogous to
1 = 0; # triggers "Can't modify constant item" error
OK, now, in an expression like this
for my $x (@X) {
# etc.
}
I assume that Perl keeps track of the index on @X correponding to
the current element. Is there a way to access this "current index"
from within a Perl program?
Thanks!
-bill
my @x = (1) x 3;
print "@x\n";
for my $y (@x) {
$y = 0;
}
print "@x\n";
__END__
1 1 1
0 0 0
It's as if the loop variable $y was a reference to the current
element of the array, except that it doesn't need to be dererenced.
Surprisingly, to me at least, this doesn't produce any errors:
for my $y (1..3) {
$y = 0;
}
Perl doesn't see the above as analogous to
1 = 0; # triggers "Can't modify constant item" error
OK, now, in an expression like this
for my $x (@X) {
# etc.
}
I assume that Perl keeps track of the index on @X correponding to
the current element. Is there a way to access this "current index"
from within a Perl program?
Thanks!
-bill