SiobhanElara
asked on
How do you delete a structure from an array?
I have an array that contains structures, and I want to delete one based on a URL variable. Let's say my array looks like the following:
ThisIsMy - Array
1. ThisIsMy - Structure: firstname, Bob; lastname: Jones; age: 30
2. ThisIsMy - Structure: firstname, Jane; lastname: Doe; age: 23
3. ThisIsMy - Structure: firstname, Sally; lastname: Smith; age: 28
I realize this has to be CF 101 stuff, but how in the world can I delete, say, the second structure in the array if I have delete=2 as a URL variable? All the code I've tried has resulted in a "complex object types cannot be converted to simple values" error or the dreaded "object of type class java.lang.Boolean cannot be used as an array."
ThisIsMy - Array
1. ThisIsMy - Structure: firstname, Bob; lastname: Jones; age: 30
2. ThisIsMy - Structure: firstname, Jane; lastname: Doe; age: 23
3. ThisIsMy - Structure: firstname, Sally; lastname: Smith; age: 28
I realize this has to be CF 101 stuff, but how in the world can I delete, say, the second structure in the array if I have delete=2 as a URL variable? All the code I've tried has resulted in a "complex object types cannot be converted to simple values" error or the dreaded "object of type class java.lang.Boolean cannot be used as an array."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Whoops, i forgot to put the array name in there.
gdemaria is correct!
gdemaria is correct!
Just incase you want to remove the structure and not delete the array element, you can just do this
<cfset ThisIsMy = [] />
<cfset s1 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset s2 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset s3 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset arrayAppend(ThisIsMy, s1) />
<cfset arrayAppend(ThisIsMy, s2) />
<cfset arrayAppend(ThisIsMy, s3) />
<cfset ThisIsMy[2] = "" />
<cfdump var="#ThisIsMy#" />
<cfset ThisIsMy = [] />
<cfset s1 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset s2 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset s3 = {firstName = 'Fred', lastName = 'Flintstone'} />
<cfset arrayAppend(ThisIsMy, s1) />
<cfset arrayAppend(ThisIsMy, s2) />
<cfset arrayAppend(ThisIsMy, s3) />
<cfset ThisIsMy[2] = "" />
<cfdump var="#ThisIsMy#" />
So if this is your array before-hand :
ThisIsMy[1].Firstname = Bob
ThisIsMy[1].LastName = Jones
ThisIsMy[1].Age = 30
ThisIsMy[2].Firstname = Jane
ThisIsMy[2].LastName = Doe
ThisIsMy[2].Age = 23
ThisIsMy[3].Firstname = Sally
ThisIsMy[3].LastName = Smith
ThisIsMy[3].Age = 28
... and you do <cfset ArrayDeleteAt(2)>
Then this is your result :
ThisIsMy[1].Firstname = Bob
ThisIsMy[1].LastName = Jones
ThisIsMy[1].Age = 30
ThisIsMy[2].Firstname = Sally
ThisIsMy[2].LastName = Smith
ThisIsMy[2].Age = 28