Can anyone see what I am doing wrong here? ```<...
# lucee
g
Can anyone see what I am doing wrong here?
Copy code
<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 100
p
IMO that is expected behavior here. structInsert() the
answersStr
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().
1
a
I presume @zackster was answering this question too. He and @pothys-mitrahsoft have nailed it. You're reusing
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
Copy code
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.
👍 1
There does not seem to be anything in the lucee docs about how the various data-types behave (which seems odd, so maybe it's just hiding from google and the table of contents?); but CF's docs kinda allude to how structs behave vis-a-vis references here: https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-lang[…]sing-arrays-and-structures/creating-and-using-structures.html
I went to check what both platforms had to say about the behaviour of the assignment operator here, but neither acknowledge there is such a thing as an assignment operator! I have nudged this on the docs subchannel
g
I have been doing a bit of Functional Programming in Scala, recently - and, so, it was just habit to use "collection"."action". I am more than happy to use struct literals, its clear ands easy to read / maintain. I remember when CF 4.5 was released... ahh the good old days - when what you typed is what you got... None of this copy value / reference "thinga-majig".... you know - when you could call yourself a web-programmer without the feintest idea of what you were really doing!!!
a
I hate to break it to you, but - whilst I cannot test your code on CF4.5 - I can test it on CF5, and it works the same way. As far as I can recollect, structs have always worked this way in CF.
If its any consolation, as far as I can tell most CFMLers still don't have the faintest idea what they are doing 😛
😀 1
g
"Oh" : I didn't say it worked they way I wanted in 4/4.5 - just reminiscing about the good old days when, at least, it seemed to be easier!
a
heeeheeeheee
z
@gavinbaumanis in response to this question, post a trycf.com example https://cfml.slack.com/archives/C06T99N9G/p1654187576612709 that's the easiest way
m
https://trycf.com/gist/15bd7eee1ad661671eb8793cccf67658/lucee5?theme=monokai added if the order of questions needs to remain in the order you added, change the first line to
questionsStruct = [:];
2
🙌 1
g
Thanks! @Matt Jones - really appreciate it.... Not sure why - it never occurred to me that I need the collection again in the output....