Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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);
 }
}
0
erikTsomik
Asked:
erikTsomik
  • 11
  • 8
1 Solution
 
_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
 
_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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
_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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now