Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Array Difference!

Status
Not open for further replies.

micjohnson

Programmer
Nov 9, 2000
86
0
0
US
Is there any logic available to find the difference between two arrays.

OldArray = "F5,PLB,REC,TOL,ZMM,ZRW"
NewArray = "F5,REC,TOL,ZMM,ZRW,ZZZ"

I need to get whats removed and whats added.

(eg. Here PLB removed and ZZZ added)

Thanks in advance.

Regards
micJ
 
this is one way to do it


<cfset myNewArray = ArrayNew(1)>
<cfset myNewArray[1] = "F5">
<cfset myNewArray[2] = "PLB">
<cfset myNewArray[3] = "REC">
<cfset myNewArray[4] = "TOL">
<cfset myNewArray[5] = "ZMM">
<cfset myNewArray[6] = "ZRW">

<cfset myOldArray = ArrayNew(1)>
<cfset myOldArray[1] = "F5">
<cfset myOldArray[2] = "REC">
<cfset myOldArray[3] = "TOL">
<cfset myOldArray[4] = "ZMM">
<cfset myOldArray[5] = "ZZZ">


<cfset myNewArrayList = ArrayToList(myNewArray, ',')>
<cfset myOldArrayList = ArrayToList(myOldArray, ",")>

<cfoutput>
<cfloop list="#myNewArrayList#" index="i">
<cfif ListContainsNoCase(myOldArrayList, i) EQ 0>
this element is added --> #i# <br>
</cfif>
</cfloop>

<cfloop list="#myOldArrayList#" index="k">
<cfif ListContainsNoCase(myNewArrayList, k) EQ 0>
this element is removed --> #k# <br>
</cfif>
</cfloop>
</cfoutput>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top