Quick thoughts on structs

June 6, 2019
Been a ColdFusion Developer since 1996
Wizard 23 posts
Followers: 13 people
Web Developer
4

Quick thoughts on structs

Been a ColdFusion Developer since 1996
Wizard 23 posts
Followers: 13 people
June 6, 2019

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:

https://cffiddle.org/app/file?filepath=8bcbbb23-90ec-44a4-9893-b835ac7fbb97/3550b62f-5bbc-4efd-ad7b-4975d25155dc/c344182b-f312-4092-8621-e14d697887a2.cfm

Comments (4)
2019-11-05 21:07:31
2019-11-05 21:07:31

What do you see as the biggest benefits of using a struct over just a list?

Like
(1)
>
rcasdorph
's comment
2019-11-06 16:34:39
2019-11-06 16:34:39
>
rcasdorph
's comment

This is a tough one. I can think of quite a few off the top of my head

  1. It is structured data as opposed to a way of interpreting a string
  2. Keys and values are discrete
  3. All kinds of member functions associated with it
  4. They naturally work with JSON
  5. They are naturally a part of the core language. Form, url, request, etc. scopes
  6. writedump() a struct is much cleaner to read than list
  7. 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.

Like
2019-06-07 23:32:28
2019-06-07 23:32:28

Attachment

I get different result from the cffiddle.

what’s the difference between 

a = [:] ;

and

a = StructNew(“ordered”, “text”); 

can’t find documentation for how to use [:] in ColdFusion

 

 

Like
2019-06-07 04:14:43
2019-06-07 04:14:43

Nice and handy!

Like
Add your comment