Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created September 18, 2012 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcameron/3743900 to your computer and use it in GitHub Desktop.
Save adamcameron/3743900 to your computer and use it in GitHub Desktop.
Function to recursively get all structkeys and convert to dot notation
<cfscript>
// data to test with
st = {
top1 = "top1 value",
top2 = {
middle = {
bottom1 = [1,2,3],
bottom2 = {},
bottom3 = "bottom3 value"
}
},
top3 = "top3 value"
};
writeDump(var=st, label="source struct");
// get the keys
keys = structToKeys(st, "st");
arraySort(keys, "textnoCase"); // sort 'em to make 'em look nice
writeDump(var=keys, label="extracted keys");
array function structToKeys(required struct struct, string parent=""){
var result = [];
for (var key in struct){
var thisPath = listAppend(parent, key, ".");
arrayAppend(result, thisPath);
if (isStruct(struct[key])){
arrayAppend(result, structToKeys(struct[key], thisPath), true);
}
}
return result;
}
</cfscript>
@adamcameron
Copy link
Author

The above example returns an array, thus:
1 st.TOP1
2 st.TOP2
3 st.TOP2.MIDDLE
4 st.TOP2.MIDDLE.BOTTOM1
5 st.TOP2.MIDDLE.BOTTOM2
6 st.TOP2.MIDDLE.BOTTOM3
7 st.TOP3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment