Three kinds of structs in ColdFusion
Here is a quick guide to all the different kinds of structs in ColdFusion
<cfscript>
// plain old struct
a = {};
a.b = “Bunny”;
a.c = “Canary”;
a.d = “Doggy”;
a.append({“a” : “Apple”});
a.append({“f” : “Frog”});
writedump(a);
// ordered
a = [:];
a.b = “Bunny”;
a.c = “Canary”;
a.d = “Doggy”;
a.append({“a” : “Apple”});
a.append({“f” : “Frog”});
writedump(a);
// sorted
a = StructNew(“ordered”, “text”);
a.b = “Bunny”;
a.c = “Canary”;
a.d = “Doggy”;
a.append({“a” : “Apple”});
a.append({“f” : “Frog”});
writedump(a);
</cfscript>
Which results in
See:
This is a tough one. I can think of quite a few off the top of my head
- It is structured data as opposed to a way of interpreting a string
- Keys and values are discrete
- All kinds of member functions associated with it
- They naturally work with JSON
- They are naturally a part of the core language. Form, url, request, etc. scopes
- writedump() a struct is much cleaner to read than list
- Items in a list can’t have commas. Structs don’t care
Even after writing these, I am sure there are others.
Lists have their charms too. When doing a form submit with checkboxes, you get lists. When querying a DB, sometimes they want lists.
You must be logged in to post a comment.