sub summs_by_key {
foreach my $R (@CURR_RECS) {
if ($combine) {
print __LINE__ . ") ... $R" ;
$R =~ s/\^/\|/ for 1 .. $KEY_POS ; # the regex expression
print __LINE__ . ") ___ $R" ;
}
#... etc
}
#... etc // no other reassingments of $R
}
Notes: @CURR_RECS is a global array declared as our @CURR_RECS ;
It is filled up in a separate sub/function.
The regex change actually can change the array values. This function
is called several times and on subsequent calls the regex change
is noticed to have acted on the elements of the array.
My question is whether that is normal behavior or a Perl bug - it only happens when the regex line is executed 2 times, not once (i.e. $KEY_POS = 2)
I fixed the problem by not using the iterator directly but a "copy" of it. I renamed the iterator to $Rs and assigned it to $R.
foreach my $Rs (@CURR_RECS) {
my $R = $Rs ;
if ($combine) {
print __LINE__ . ") ... $R" ;
$R =~ s/\^/\|/ for 1 .. $KEY_POS ;
print __LINE__ . ") ___ $R" ;
}
# ... etc
}
@CURR_RECS contains
2x^szo_p^230414:01:43^0^0^
3x^cold^230309p^0^0^QQ.y
3x^szo_p^230419:14:51^-10^6^
4x^cold^230320^0^60^
4x^front^230418:16:36^20^40^
4x^sky^230403^0^0^
4x^szo_r^230419:14:46^-5^23^
6g^szo_p^230309^0^8^
6x^cup^230417:05:04^2^4^
6x^r_l^230402^0^40^
where the carets are field separator markers. If $combine is true then we want to combine the first so many fields ... that is what the regex accomplishes ... by changing so many ($KEY_POS) of the separators to something else - into "|". So these are the before and after the regex entries:
125) ... 2m^szo_p^230411:15:01^0^0^
127) ___ 2m|szo_p|230411:15:01|0^0^
125) ... 2x^szo_p^230414:01:43^0^0^
127) ___ 2x|szo_p|230414:01:43|0^0^
125) ... 3x^cold^230309p^0^0^QQ.y
127) ___ 3x|cold|230309p|0^0^QQ.y
125) ... 3x^szo_p^230419:14:51^-10^6^
127) ___ 3x|szo_p|230419:14:51|-10^6^
However when the same function is executed again the incoming elements are not the same as in the first run ... but have been modified per the regex. The leading 126) ...
and 127) ___
are not part of the array they are LINE numbers of the code. ON the second calling of the function we can see that the incoming elements show the carets moved by the earlier invocation.
121) ... 2m|szo_p|230411:15:01|0^0^
123) ___ 2m|szo_p|230411:15:01|0|0|
Use of uninitialized value $V in addition (+) at /media/pk/u2win/dev/pkp.db/pkp.summ.pl line 132.
121) ... 2x|szo_p|230414:01:43|0^0^
123) ___ 2x|szo_p|230414:01:43|0|0|
Use of uninitialized value $V in addition (+) at /media/pk/u2win/dev/pkp.db/pkp.summ.pl line 132.