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
Solved

Delete elemnt of the array in coldfusion

Posted on 2011-09-08
9
320 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
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

789 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