Up to version 7, the value we got used to be a pointer to the subarray.
Modifying some element of the subarray would persist when exiting the loop.
Starting in version 7, we get a local clone of the subarray.
Any modifications done are lost once outside the foreach loop.
I did not see any mention of such a change anywhere.
Did I miss something or am I doing something wrong ?
Here is code to illustrate the issue:
Code: Select all
# the mother array
:global aGlobalArray ({});
# function to add a sub-array, defined locally, to the global array
:global popData do={
:global aGlobalArray;
:local subObj ({soname="soname-orig"; sodata="sodata-orig"});
:set ($aGlobalArray->"sub-obj-1") $subObj;
:return "OK";
}
:log info "Populate and log";
:local pop [$popData];
:foreach key,value in=$aGlobalArray do={
:log info ("[key:".$key."] soname:".($value->"soname")." | sodata:".($value->"sodata"));
# result [key:sub-obj-1] soname:soname-orig | sodata:sodata-orig
}
:log info "Modify subjobj and log";
:foreach key,value in=$aGlobalArray do={
:set ($value->"sodata") "sodata-modified";
# logging the data here shows it is modified
# Adding this line fixes the issue for v7 but is not needed in v6
# :set ($aGlobalArray->"$key") $value;
}
:foreach key,value in=$aGlobalArray do={
:log info ("[key:".$key."] soname:".($value->"soname")." | sodata:".($value->"sodata"));
# v6 result [key:sub-obj-1] soname:soname-orig | sodata:sodata-modified
# v7 result [key:sub-obj-1] soname:soname-orig | sodata:sodata-orig
}