Solved

Delete elemnt of the array in coldfusion

Posted on 2011-09-08
9
319 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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.

785 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