Solved

Delete elemnt of the array in coldfusion

Posted on 2011-09-08
9
317 Views
Last Modified: 2012-05-12
I have 2 arrays . Let call it arr1 and arr2.

I need to delete all ites from arr1 that are not in arr2
0
Comment
Question by:erikTsomik
  • 5
  • 3
9 Comments
 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
I am using coldfusion 9
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I wouldn't bother deleting the elements.  Just create a new array containing only the elements found in #arr2#. Then use that array instead.

<cfset arr1       = ["a", "c", "f", "g"]>
<cfset arr2       = ["a", "b", "c", "e"]>
<cfset newArray = []>
<cfloop array="#arr1#" index="e1">
      <cfif arrayFindNocase(arr2, e1)>
            <cfset arrayAppend(newArray, e1)>
      </cfif>
</cfloop>

<cfdump var="#newArray#">
0
 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
well i am passing my array by reference so I need to remove items from the array 1
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Are you sure about that? Unlike structures, array's usually are not passed by reference.  Not unless they're nested in a structure or something.  

If you really do need a delete, loop through it in reverse. Then delete non-matching elements.

<cfset arr1       = ["a", "c", "f", "g"]>
<cfset arr2       = ["a", "b", "c", "e"]>
<cfloop from="#arrayLen(arr1)#" to="1" index="x" step="-1">
      <cfoutput>#arr1[x]#</cfoutput>
     <cfif not arrayFindNocase(arr2, arr1[x])>
           <cfset arrayDeleteAt(arr1,x)>
      </cfif>
</cfloop>

<cfdump var="#arr1#">
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
why do we use step -1

I am using cfscript how can i add step to it
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Did you verify your array really is passed by reference? If it's not ... your delete won't work anyway.

>> why do we use step -1

Each time you delete, the size of the array changes.  So you must delete in reverse to avoid trying to delete elements that no longer exist. That'll cause an error,.


>> I am using cfscript how can i add step to it

     I'm not sure cfscript supports "step".
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
>>  I'm not sure cfscript supports "step".

Well .. you don't really need it. Just decrement instead of increment

for (x = arrayLen(arr1); x >= 1; x--) {
    ... do stuff
}
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Here the "do stuff"
<cfscript>
arr1 = ["a", "c", "f", "g"];
arr2 = ["a", "b", "c", "e"];
for(x=ArrayLen(arr1);x GE 1; x--){
  WriteOutput(arr1[x]);
  if(!ArrayFindNoCase(arr2, arr1[x])){
    ArrayDeleteAt(arr1,x);
  }
}
</cfscript> 

<cfdump var="#arr1#">

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Actually remove the writeoutput.  It was just debugging  code that's not doing anything.

<cfscript>
     arr1 = ["a", "c", "f", "g"];
     arr2 = ["a", "b", "c", "e"];
     for(x=ArrayLen(arr1);x GE 1; x--){
        if(!ArrayFindNoCase(arr2, arr1[x])){
          ArrayDeleteAt(arr1,x);
        }
     }
</cfscript>
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now