gavinbaumanis
06/02/2022, 7:39 AM<cfscript>
answersStr = {};
resultStr = {};
//Struct for answer 1
answersStr.insert("value", 30);
answersStr.insert("description", "Q1 Desc.");
//Add Answer 1 to result Struct
resultStr.insert("Q1", answersStr);
answersStr.clear();
answersStr.insert("value", 100);
answersStr.insert("description", "Q2 Description");
resultStr.insert("Q2", answersStr);
writeDump(resultStr);
</cfscript>
It gives the following output
What happened to the values for Q1?
They obviously got overridden by Q2 - but why?
If I dump it directly after the Q1 insert into resultStr I get Q1's correct data.
Struct
Q1 Struct
description string Q2 Description
value number 100
Q2 Struct
description string Q2 Description
value number 100pothys-mitrahsoft
06/02/2022, 8:35 AManswersStr to resultStr.Q1, it insert answersStr variable reference, not just the value, So resultStr.Q1 reference answersStr. any change made in answersStr will reflect in resultStr.Q1. Here you have to use structCopy() or duplicate().Adam Cameron
answersStr, so blitzing the original values when you clear it and reassign its properties. This is expected behaviour (both Lucee and CF btw, @zackster).
I would probably revise yer code to use struct literals rather than insert and clear, eg something along these lines:
https://trycf.com/gist/041d070e8618c73ae28bc831dbe74ef7/lucee5?theme=monokai
resultStr = {};
answersStr = {
"value" = 30,
"description" = "Q1 Desc."
};
resultStr["Q1"] = answersStr;
answersStr = {
"value" = 100,
"description" = "Q2 Description"
};
resultStr["Q2"] = answersStr
writeDump(resultStr);
IMO putting stuff into a struct via insert is kind of a 2000s approach to such things, for most situations other than a coupla edge-cases.Adam Cameron
Adam Cameron
gavinbaumanis
06/02/2022, 11:42 AMAdam Cameron
Adam Cameron
gavinbaumanis
06/02/2022, 11:58 AMAdam Cameron
zackster
06/02/2022, 4:56 PMMatt Jones
06/02/2022, 5:16 PMquestionsStruct = [:];gavinbaumanis
06/03/2022, 12:07 AM