14 Replies Latest reply on Jul 9, 2010 2:13 AM by karthic_vkn

# Compare Two Array

Hi,

can you plz help me.

How i can compare two array. following my code..

var my_array:Array=Array(5,10,15,20,25,30)
var new_array:Array=Array(7,8,9,10,11,12,13)
for(var i:Number=0;i<my_array.length;i++){
if(my_array[i]==new_array[i]){
trace(my_array[i])
}
}

JaxNa

• ###### 1. Re: Compare Two Array

How do you think to compare, the value of the each array needs to compare?

• ###### 2. Re: Compare Two Array

yes i want compare value of the each array

• ###### 3. Re: Compare Two Array

Then you are almost on correct... See the same code which I modified little bit,

var my_array:Array = Array(5, 10, 15, 20, 25, 30);
var new_array:Array = Array(7, 10, 9, 10, 10, 30, 13);

for (var i:Number = 0; i<my_array.length; i++) {
if (my_array[i] == new_array[i]) {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Matching");
} else {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Not Matching");
}
}

If its not so, explain exactly what you want

• ###### 4. Re: Compare Two Array

Thanks a lot dear for your kindly replay ..

Thanks Karthic

• ###### 5. Re: Compare Two Array

hey dear can you tell me one thing..

when my_array length is 3

and new_array length is 5

that time how i can compare two array actually i have this problem..

var my_array:Array = Array(5, 10, 15, 20);
var new_array:Array = Array(7, 8, 9, 10, 11, 12);

for (var i:Number = 0; i<my_array.length; i++) {
if (my_array[i] == new_array[i]) {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Matching");
} else {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Not Matching");
}
}

Thanks

• ###### 6. Re: Compare Two Array

If array has different length, then obviously the comparison will not work so the comparison must be false, that case you need to check whether the array has same length or not...

if (my_array.length == new_array.length) {

for (var i:Number = 0; i<my_array.length; i++) {

if (my_array[i] == new_array[i]) {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Matching");
} else {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Not Matching");
}

}

} else {

trace("array length is not matching");
}

• ###### 7. Re: Compare Two Array

ya thats right.

when two array's length is not matching that time how i can compare array's value..

new_array is static array and my_array is dynamic array.

can u give me any solution??

Thanks dear,

JaxNa

• ###### 8. Re: Compare Two Array

Then you have to find out which array has minimum length, since we restrict ‘for’ loop to run unnecessarily.

Here some stuff I added (we can do this in many ways).

var my_array:Array = Array(5, 10, 15, 20, 1);
var new_array:Array = Array(7, 8, 9);
var findMinArrayName:String;

if (new_array.length<my_array.length) {
compareTwoArrays("new_array");
} else {
compareTwoArrays("my_array");
}

function compareTwoArrays(findMinArrayName) {
for (var i:Number = 0; i<this[findMinArrayName].length; i++) {
if (my_array[i] == new_array[i]) {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Matching");
} else {
trace("'My Array' "+i+"("+my_array[i]+") 'new_array' "+i+"("+new_array[i]+") = Not Matching");
}
}
}

• ###### 9. Re: Compare Two Array

its not working dear

• ###### 10. Re: Compare Two Array

Code is not working or else someother problem ?...

• ###### 11. Re: Compare Two Array

when i take this array

var my_array:Array = Array(5, 10, 15, 20, 1);
var new_array:Array = Array(7, 8, 9,10);

then i didn't get "10" value

Thanks,

JaxNa

• ###### 12. Re: Compare Two Array

You have to explain what you exactly want..

Try this...

var my_array:Array = Array(5, 10, 15, 20, 1);
var new_array:Array = Array(7, 1, 20, 10);

for (var i:Number = 0; i<new_array.length; i++) {
for (var a:Number = 0; a<my_array.length; a++) {
if (new_array[i] == my_array[a]) {
trace("'new_array' "+i+"("+new_array[i]+") 'my_array' "+a+"("+my_array[a]+") = Matching");
}
}

}

• ###### 13. Re: Compare Two Array

thanks alot dear.....

sory for i gave u headch....

THANKS

JaxNa

• ###### 14. Re: Compare Two Array

you r welcome !!!