[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Delete elemnt of the array in coldfusion

Posted on 2011-09-08
9
Medium Priority
?
333 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

649 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