DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 5 · LESSON 08

Adding, Removing & Checking Struct Keys

Modifying and inspecting ColdFusion structs — structInsert, structDelete, structClear, structAppend for merging, structKeyExists, structIsEmpty, structCount, and the shallow-vs-deep copy distinction.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

Beyond simple assignment (user.name = "John"), ColdFusion has dedicated functions for inserting, removing, merging, and checking struct contents — useful when the key itself is dynamic, or when you need to safely check something exists before using it.

Learning Objectives

After completing this lesson, you'll be able to:

  • Insert and delete struct keys with structInsert and structDelete.
  • Clear all of a struct's data with structClear.
  • Merge two structs together with structAppend.
  • Check for a key's existence with structKeyExists.
  • Check struct size and emptiness with structCount and structIsEmpty.
  • Understand the difference between a shallow copy and a deep copy.

Inserting a Key: structInsert

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

structInsert(struct, key, value) errors if the key already exists — use dot/bracket assignment instead if you want to overwrite silently.

Removing a Key: structDelete

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Clearing All Data: structClear

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Merging Structs: structAppend

Copies every key from a source struct into a destination struct. The third argument controls what happens when both structs share a key — true (the default) overwrites, false keeps the destination's original value.

CFScript — overwrite (default)
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
CFScript — keep original
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

In the second example, b stays 0 because false means "don't let options overwrite config's existing keys."

Checking a Key Exists: structKeyExists

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Always check structKeyExists before reading an optional key — accessing a missing key directly (user.email) throws an error rather than returning blank.

Counting and Checking Emptiness

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Checking for a Struct: isStruct

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Shallow Copy vs. Deep Copy

Assigning a struct to a new variable (newVar = originalStruct) doesn't copy it — both variables point to the same struct, so changing one changes the other. structCopy() makes a real copy, but only at the top level — nested structs inside it are still shared. duplicate() makes a full, independent deep copy.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Notice original.address.city ends up as "Mumbai", not "Chennai" — the shallow copy's nested address struct was never truly separate from the original, so changing it through shallow also changed original. The deep copy (deep) is fully independent instead.

Real-World Example: Merging Default Settings

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Copying defaults first avoids mutating the original defaults struct — a common pattern when combining app-wide defaults with a specific user's overrides.

Common Beginner Mistakes

Accessing a possibly-missing key without checking first

user.email throws an error if email was never set. Check structKeyExists(user, "email") first, or use structKeyExists in combination with a fallback value.

Assuming assignment copies a struct

newStruct = originalStruct doesn't create a copy — both variables reference the same struct. Use structCopy() (shallow) or duplicate() (deep) when you actually need an independent copy.

Assuming structCopy() makes nested structs independent too

structCopy() only copies the top level. A nested struct inside the copy is still the same object as the nested struct in the original — use duplicate() for full independence.

Best Practices

  • Always structKeyExists() before reading an optional or user-supplied key.
  • Use duplicate() rather than structCopy() whenever the struct has nested structs or arrays you might modify independently.
  • Use structAppend() with false to layer user overrides on top of defaults without letting the defaults silently win.

Interview Questions

What's the difference between structCopy() and duplicate()?

structCopy() makes a shallow copy — only the top-level keys are independent; nested structs/arrays are still shared. duplicate() makes a full, independent deep copy.

What does structAppend's third argument control?

Whether the source struct's values overwrite the destination struct's values when both share a key. true (the default) overwrites; false keeps the destination's original values.

Why check structKeyExists before accessing a key?

Accessing a key that doesn't exist throws an error rather than returning a blank or null value.

What does simply assigning one struct variable to another do?

It doesn't copy anything — both variables end up referencing the exact same struct, so a change through either one is visible through both.

Summary

In this lesson, you learned to insert and delete struct keys, clear a struct, merge two structs with structAppend, check for keys and emptiness, and the crucial difference between a shallow copy (structCopy) and a deep copy (duplicate).

What's Next?

The next lesson covers functional struct operations — structEach, structMap, structFilter, and structReduce.