Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array delete help

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
How can I delete an array that has empty values?

I have an array that has many elements. I want to loop through the array and delete an array that has empty elements.

For example, I tried this and it doesn't delete any empty arrays:
Code:
<cfloop index="OfferCounter1" from="1" to="#Arraylen(OfferArray)#">
  <cfif OfferArray[OfferCounter1] is 0>
    <cfset OfferArray = arraydeleteat(OfferArray, 1)>
  </cfif>
</cfloop>

Now if the third and seventh array has empty elements how can I delete those arrays?

Thanks.

[sub]
____________________________________
Just Imagine.
[sub]
 
when you say empty i think of no value in which case the element may already not exist.

your cfif says "0" so is your definition of "empty" meaning to store the value of "0"?

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
and
<cfset OfferArray = arraydeleteat(OfferArray, 1)>

will change OfferArray from type Array to type boolean.

you should use something like
Code:
<cfset [b]temp[/b] = arraydeleteat(OfferArray, 1)>

temp will return "YES" and your offerArray will be one less array element.

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
No, I mean when the array has empty elements. I do a CFDUMP of the array and I get this for the empty elements:

2 array
1 8532
2 377
3 221
4 Question 123
5 1
6 0
7 none
8 1
====
3 array
1 [empty string]
2 [empty string]
3 [empty string]
4 [empty string]
5 [empty string]
6 [empty string]
7 [empty string]
8 [empty string]


[sub]
____________________________________
Just Imagine.
[sub]
 
so loop through the array and delete anything with "" not "0"

you'll have to loop through each array seperately. untested:
Code:
<cfloop from = "1" to = "#arrayLen(thisArray)#" index = "x">
    <cfif thisArray[x]# eq "">
      <cfset temp = arrayDeleteAt(thisArray, x)>
    </cfif>
</cfloop>

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
Get an error:
Code:
Complex object types cannot be converted to simple values.  
The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. 
The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a <CFIF> tag. This was possible in ColdFusion 2.0 but creates an error in later versions. 
 
  
The error occurred in C:\DOCUMENTS\[URL unfurl="true"]WWWROOT\ccusa\cc\start.cfm:[/URL] line 426
 
424 : 
425 : <cfloop from = "1" to = "#arrayLen(OfferArray)#" index = "OfferCounter1">
426 :     <cfif #OfferArray[OfferCounter1]# eq "">
427 :       <cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
428 :     </cfif>

I am using:
Code:
<cfloop from = "1" to = "#arrayLen(OfferArray)#" index = "OfferCounter1">
    <cfif #OfferArray[OfferCounter1]# eq "">
      <cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
    </cfif>
</cfloop>

[sub]
____________________________________
Just Imagine.
[sub]
 
did you try it without the pound signs?
Code:
<cfloop from = "1" to = "#arrayLen(OfferArray)#" index = "OfferCounter1">
    <cfif OfferArray[OfferCounter1] eq "">
      <cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
    </cfif>
</cfloop>


Hope This Helps!

ECAR
ECAR Technologies, LLC

"My work is a game, a very serious game." - M.C. Escher
 
Yup, tried it and got the same error.

I even tried:
<cfloop index="OfferCounter1" from="1" to="#Arraylen(OfferArray)#">
<cfif OfferArray[OfferCounter1][1] is "">
<cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
</cfif>
</cfloop>

OfferArray[OfferCounter1][1] means the first element in OfferArray's index is blank. And then I do that I get:

The element at position 2, of dimension 2, of an array object used as part of an expression, cannot be found.


The error occurred in C:\DOCUMENTS\ line 426

424 :
425 : <cfloop index="OfferCounter1" from="1" to="#Arraylen(OfferArray)#">
426 : <cfoutput>#OfferArray[OfferCounter1][2]#</cfoutput>
427 : <cfif OfferArray[OfferCounter1][1] is "">
428 : <cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>

[sub]
____________________________________
Just Imagine.
[sub]
 
your last post wont work because you're trying to use it like a 2d array when it's only a 1d array.

I tested the following code and it works ok.

Code:
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "Travis">
<cfset myArray[2] = "joe">
<cfset myArray[3] = "alex">
<cfset myArray[4] = "steve">
  <cfloop from = "1" to = "#arrayLen(myArray)#" index = "x">
    <cfif myArray[x] eq "">
      <cfset temp = arrayDeleteAt(myArray, x)>
    </cfif>
  </cfloop>
<cfoutput>

the problem is if there any blanks it will throw an error.

lets say element 2 is "". when you remove the array element you now only have 3 elements to the array. the loop will run for times so "position 4 of array "myArray" could not be found"

so try this:
Code:
<cfset myArray = arrayNew(1)>
<cfset myArray[1] = "Travis">
<cfset myArray[2] = "">
<cfset myArray[3] = "alex">
<cfset myArray[4] = "steve">
<cfoutput>#arrayLen(myarray)#</cfoutput><br>
  <cfloop from = "1" to = "#arrayLen(myArray)#" index = "x">
    <cfif [b]x lte arrayLen(myArray) and [/b]myArray[x] eq "">
      <cfset temp = arrayDeleteAt(myArray, x)>
    </cfif>
  </cfloop>
<cfoutput>
#arrayLen(myArray)#
</cfoutput>

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
Hmmm, sorry I made a mistake in my original post. Maybe cause its Monday or maybe i'm going in all directions. The array is a 2-d array.

Is deleting an array still doable in a 2d array??

Again sorry...

[sub]
____________________________________
Just Imagine.
[sub]
 
yeah, see now we're playing a new game...

livedocs said:
You can use ArrayDeleteAt with multi-dimensional arrays. However, it will delete an entire row of the array.
<h3>ArrayDeleteAt Example</h3><p>
<!--- create an array --->
<cfset DaysArray = ArrayNew(2)>
<!--- populate an element or two --->
<cfset DaysArray[1][1] = "Monday">
<cfset DaysArray[2][1] = "Tuesday">
<cfset DaysArray[3][1] = "Wednesday">
<cfset DaysArray[1][2] = "April 12">
<cfset DaysArray[2][2] = "April 13">
<cfset DaysArray[3][2] = "April 14">
<!--- delete the second element --->
<p>This is what the array looks like before delete:<br>
<cfoutput>
#DaysArray[1][1]#&nbsp;&nbsp;#DaysArray[1][2]#<br>
#DaysArray[2][1]#&nbsp;&nbsp;#DaysArray[2][2]#<br>
#DaysArray[3][1]#&nbsp;&nbsp;#DaysArray[3][2]#<br>
</cfoutput>

<cfoutput>
We delete this element of the array:<br>
#ArrayDeleteAt(DaysArray,2)#<br>
</cfoutput>
<!--- the formerly third element, "Wednesday" is second element --->
<p>This is what the array looks like after delete:<br>
<cfoutput>
#DaysArray[1][1]#&nbsp;&nbsp;#DaysArray[1][2]#<br>
#DaysArray[2][1]#&nbsp;&nbsp;#DaysArray[2][2]#<br>
</cfoutput>

in a multi dimentional array the whole row is gone (two elements in one shot in this example.) since they used delete at (2) all of the elements are gone where the first dimention is 2.

so if you have
myArray[2][1] = "a"
myArray[2][2] = "b"
myArray[2][3] = "c"
myArray[2][4] = "d"
myArray[2][5] = "e"
myArray[2][6] = "f"
myArray[2][7] = "g"
arrayDeleteAt(myArray, 2) would delete all of them.

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
See, this is what my process is:

I have 2d arrays. I loop through the arrays and delete the elements if they are not what I want. Then I loop over the new array and try to delete the array itself if it has any empty elements.

I am now using:
Code:
<cfloop from="1" to="#arrayLen(OfferArray)#" index="OfferCounter1">
  <cfif OfferCounter1 lte arrayLen(OfferArray) and OfferArray[OfferCounter1] eq "">
    <cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
  </cfif>
</cfloop>

This throws an error:
Code:
Complex object types cannot be converted to simple values.  
The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. 
The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a <CFIF> tag. This was possible in ColdFusion 2.0 but creates an error in later versions. 
 
  
The error occurred in C:\DOCUMENTS\[URL unfurl="true"]WWWROOT\ccusa\cc\start.cfm:[/URL] line 416
 
414 : <cfdump var="#OfferArray#" label="offerarray">
415 : <cfloop from = "1" to = "#arrayLen(OfferArray)#" index = "OfferCounter1">
416 : 	<cfif OfferCounter1 lte arrayLen(OfferArray) and OfferArray[OfferCounter1] eq "">
417 : 		<cfset temp = arrayDeleteAt(OfferArray, OfferCounter1)>
418 : 	</cfif>

However, if I remove the and OfferArray[OfferCounter1] eq , it now deletes but in a wierd way. If I have 9 arrays, and 2 are blanks instead of displaying 7 it displays 4.



[sub]
____________________________________
Just Imagine.
[sub]
 
The only problem is unlike the example you posted I have no way of knowing what position I want to delete. It can be postion 2, or position 6, or position 5,7 and 9.

I tried this:
Code:
<cfloop from="1" to="#arrayLen(OfferArray)#" index="OfferCounter1">
  <cfif OfferCounter1 lte arrayLen(OfferArray)>
    <cfset temp = arrayDeleteAt(OfferArray[OfferCounter1], OfferCounter1)>
  </cfif>
</cfloop>

And I get this error:
Code:
Cannot insert/delete at position 9.  
The array passed has 8 indexes. Valid positions are from 1 to 8.  
  
The error occurred in C:\DOCUMENTS\[URL unfurl="true"]WWWROOT\ccusa\cc\start.cfm:[/URL] line 417
 
415 : <cfloop from="1" to="#arrayLen(OfferArray)#" index="OfferCounter1">
416 : 	<cfif OfferCounter1 lte arrayLen(OfferArray)>
417 : 		<cfset temp = arrayDeleteAt(OfferArray[OfferCounter1], OfferCounter1)>
418 : 	</cfif>
419 : </cfloop>

Originally I had 9 arrays. 2 should be deleted cause they have empty elements. So I should be getting back 7.

What am I to do?

[sub]
____________________________________
Just Imagine.
[sub]
 
I take it we posted about the same time... read my last post.

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
the reason you're getting the "Complex object types cannot be converted to simple values. " error is because you're trying to use a 2d array like a 1d array. when you compare you have to use the full location.

<cfset myArray[1][1] = "bob">

<cfif myArray[1] = "bob"> do something </cfif>
that will throw an error because you have to look at
myArray[1][1]

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
I dont follow. Arrays is confusing to me. Since they used arrays since the begining of time, i'm forced to modify the arrays.

arraydeleteat(OfferArray,3) should delete the third array, right? I tried this:
Code:
<cfloop from="1" to="#arrayLen(OfferArray)#" index="OfferCounter1">
	<!---cfif OfferArray lte arrayLen(OfferArray) and offerarray is ""--->
		<cfset temp = arrayDeleteAt(OfferArray, 3)>
	<!---/cfif--->
</cfloop>

And I get this:
Code:
Cannot insert/delete at position 3.  
The array passed has 2 indexes. Valid positions are from 1 to 2.  
  
The error occurred in C:\DOCUMENTS\[URL unfurl="true"]WWWROOT\ccusa\cc\start.cfm:[/URL] line 417
 
415 : <cfloop from="1" to="#arrayLen(OfferArray)#" index="OfferCounter1">
416 : 	<!---cfif OfferArray lte arrayLen(OfferArray) and offerarray is ""--->
417 : 		<cfset temp = arrayDeleteAt(OfferArray, 3)>
418 : 	<!---/cfif--->
419 : </cfloop>

Shouldn't it just delete array 3 and move on?



[sub]
____________________________________
Just Imagine.
[sub]
 
ok first you only have 1 array. you have positions and dimentions in your array.

a single dimention is one column.

1
2
3
4
5

a 2 dimentional array is like a spreadsheet with x and y axis.
1234
1abcd
2efgh
3ijkl
4mnop
in position [2][2] you'll find the value of "f". see? in [3][4] you get "l"

3 dimentions are a bit more confusing. it's a 3 dimentional cube if you were to picture it. with x y and z axis

in this case if you were to say arraydelete(my2darray, 2) you would remove values e,f,g, and h.

it looks like in your error you only have two rows. (1 and 2) so there is no position 3 to delete.

Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand. - Putt's Law
 
Thanks for the quick tutorial. That did help. Turns out arraydeleteat() doesn't work for me. I asked another developer and he just suggested to re-write the array instead of trying to delete it.

I will post the code I used tomorrow just in case someone else in the future sees this thread and needs a solution.

Thanks!

[sub]
____________________________________
Just Imagine.
[sub]
 
Hi GUJUm0deL,

It's because you are doing a cfloop, with the FROM criteria being the Length of the Array.... it's successfully deleting the array where you want it to, but then erroring when it tries to keep looping around. If you try hardcoding it in without the loop, you'll find it works just fine!
 
Junkjones, not it wont! He's using a single multidimentional array. You stopped reading after the 5th post, before we came to that realization...

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top