Structures can contain other structures, much like arrays can contain arrays
<cfscript>
//Create a structure container
loginInfo = structNew();
//Create a key in the structure container which contains another struct of data
loginInfo["User" & 01] = structNew();
//Stick some data into the nested structure
loginInfo["User" & 01]["username"] = "mrWill";
loginInfo["User" & 01]["password"] = "myPassword";
loginInfo["User" & 02] = structNew();
loginInfo["User" & 02]["username"] = "mrLeroy";
loginInfo["User" & 02]["password"] = "leroysPassword";
</cfscript>
Produces this:
| struct |
| User01 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User02 |
| struct |
| password |
leroysPassword
|
| username |
mrLeroy
|
|
When you see a pattern as above, you can see the potential for looping over data to build structs of structs
<cfscript>
//Create a structure container
loginInfo = structNew();
//Loop ten times, simulating ten users
for(i = 1; i lte 10; i = i + 1){
//Stick some data into the nested structure
loginInfo["User" & "0" & i]["username"] = "mrWill";
loginInfo["User" & "0" & i]["password"] = "myPassword";
}
</cfscript>
Produces this:
| struct |
| User01 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User010 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User02 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User03 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User04 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User05 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User06 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User07 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User08 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
| User09 |
| struct |
| password |
myPassword
|
| username |
mrWill
|
|
Whoa!! Structures can even contain arrays!
<cfscript>
//Create two arrays of info
aryLoginInfo = arrayNew(1);
aryLoginInfo[1] = "Will";
aryLoginInfo[2] = "Leroy";
aryLoginInfo2 = arrayNew(1);
aryLoginInfo2[1] = "David";
aryLoginInfo2[2] = "Michael";
//Create a structure with two keys, and stick each array into a key
loginInfo = structNew();
loginInfo["User" & 01] = aryLoginInfo;
loginInfo["User" & 02] = aryLoginInfo2;
</cfscript>
Produces this: