ArrayFind issue in coldfusiion

I have 2 arrays.

and i need to delete from
Array1 all those IDthat are not in Array 2

Here is what I doing , but it deletes the entire Array1

for (itt = ArrayLen(Array1);itt gte 1;itt--) {
 if  (ArrayFind(Array2, Array1[itt]) == 0) {
      arrayDeleteAt(Array1,itt);
 }
}
LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?
 
_agx_Commented:
> the values in my arrays are numbers.

Case sensitivity doesn't affect numbers.  

Btw, we could've answered this MUCH quicker if you'd just posted a small sample. They're only numbers! lol
0
 
_agx_Commented:
The only way it would delete everything is if none of the elements matched.  Here's a demo showing there's nothing wrong with your code. The final result is:

     Array1 = 5, 10, 20

<cfscript>
      Array1 = [5,10,15,20,25];
      Array2 = [5,10,20,23];
      
      for (itt = ArrayLen(Array1);itt gte 1;itt--) {
             if  (ArrayFind(Array2, Array1[itt]) == 0) {
                  arrayDeleteAt(Array1,itt);
             }
      }
      WriteDump(Array1);
</cfscript>
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
that is exactly what I am doing , But it removes everything instead
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
_agx_Commented:
I don't see any way that could happen unless of course none of the elements matched. Which is ok, because that's what you're asking it to do. Dump both arrays. What are the values?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I do not know if this is a problem I define array as a Java Array
0
 
_agx_Commented:
Maybe.  Can you post a simple case that demo's the issue (including the java array code)?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
this is how I declare my array1

array1= CreateObject("java","java.util.ArrayList").init();
0
 
_agx_Commented:
Nope. Works fine with an ArrayList too. (Though I'm not sure why you even need one over a CF array).

You're going to have post a dump a of the actual values causing a problem. Or some demo code I can use to reproduce the problem.  Because so far, there's absolutely nothing wrong with the code you posted ;-)
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I found the issue. The issue was that Java array did not work with ArrayFind so I turn Array1 to the list and did ArrayFind by ListGetat then it works

Very weird.

But when I try to do this it did not work

var order  = Array1[itt];
                                                                  
                                                                  
                                                                  
                                                                  if (ArrayFind(tempArr,order) == 0){
0
 
_agx_Commented:
> The issue was that Java array did not work with ArrayFind

I highly doubt that as CF arrays *are* java.util.ArrayList objects ;-)  What version are you using?

Here's another demo to show it works.

<cfscript>
      Array1 = createObject("java", "java.util.ArrayList").init();
      Array2 = createObject("java", "java.util.ArrayList").init();
      ArrayAppend(Array1, 5);
      ArrayAppend(Array1, 10);
      ArrayAppend(Array2, 10);
     
      for (itt = ArrayLen(Array1);itt gte 1;itt--) {
             if  (ArrayFind(Array2, Array1[itt]) == 0) {
                  arrayDeleteAt(Array1,itt);
             }
      }
      WriteDump(Array1);
</cfscript>
0
 
_agx_Commented:
> *are* java.util.ArrayList objects

Correction:   java.util.List objects
0
 
_agx_Commented:
My guess is you're searching for strings and maybe forgot ArrayFind() is case sensitive. ListGetAt isn't. But since you never posted any sample code ... that's just a guess.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
the values in my arrays are numbers. I can use ArrayFindNoCase
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
If I use

var order  = Int(Array1[itt]);

Then it works too
0
 
_agx_Commented:
That has nothing to do with case sensitivity.  It's the difference between a string comparison and a numeric comparison.  It's not obvious, but if you do a STRING search for an array containing

      Array[1] = 55

It will only match "55" exactly. It won't match the string "55.0" or "55.0000".  

It's one of the gotchas of using a typeless language like CF.



0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I will only have numbers like this 55
0
 
_agx_Commented:
Well ... apparently not if you say ArrayFind wasn't working ;) If you test any of the examples with two instances of "55" it works just fine. So obviously something is different about your array values than you're mentioning.  But without being able to see any data, it's impossible to say what the difference is ...

Next time, if the values are just numbers (and not anything proprietary) - please post them up front. You'll get a solution much quicker if we don't have to keep guessing ;)




0
 
erikTsomikSystem Architect, CF programmer Author Commented:
well the only difference is array1 declared as a javaArray and array2 declared as coldfusion array
0
 
_agx_Commented:
That worked fine too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.