Solved

Delete elemnt of the array in coldfusion

Posted on 2011-09-08
9
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 19

Author Comment

by:erikTsomik
ID: 36504363
I am using coldfusion 9
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36504722
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
ID: 36504791
well i am passing my array by reference so I need to remove items from the array 1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:_agx_
ID: 36504928
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 36505525
why do we use step -1

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

Expert Comment

by:_agx_
ID: 36505667
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
ID: 36505695
>>  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
ID: 36505741
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_
ID: 36505833
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coldfusion: Two dynamic depending drop down 12 55
Question on web subscription for apps 3 122
paging 3 53
Handling onsession end function in Application.cfc 5 75
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 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