Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

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);
 }
}
Avatar of _agx_
_agx_
Flag of United States of America image

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>
Avatar of erikTsomik

ASKER

that is exactly what I am doing , But it removes everything instead
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?
I do not know if this is a problem I define array as a Java Array
Maybe.  Can you post a simple case that demo's the issue (including the java array code)?
this is how I declare my array1

array1= CreateObject("java","java.util.ArrayList").init();
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 ;-)
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){
> 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>
> *are* java.util.ArrayList objects

Correction:   java.util.List objects
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.
the values in my arrays are numbers. I can use ArrayFindNoCase
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I use

var order  = Int(Array1[itt]);

Then it works too
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.



I will only have numbers like this 55
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 ;)




well the only difference is array1 declared as a javaArray and array2 declared as coldfusion array
That worked fine too.