Skip to main content
June 13, 2007
Question

comparing contents between two arrays

  • June 13, 2007
  • 9 replies
  • 881 views
Hi. I've tried to find this on my own, but have not had much luck, so I hope somebody can help me out.
I have 3 arrays. All three have a set of numbers(integers) in it.

1. Is there a way to compare arrayA with arrayB to see if there are any matching numbers between the two of them? They don't have to be at the same index number, just anywhere within the arrays. ?

2. And then, if there are matching numbers, take that number(s) and see if it exists anywhere within a third array (arrayC). If it does, then I want to swap out that number from the third array and replace it with something else. I think I know how to do the swapping, I'm just not sure how to capture that value from the first step.

Thank you very much for any help you can give me.

--cindy
This topic has been closed for replies.

9 replies

Inspiring
June 13, 2007
@kglad : looks good and I think I understand it too. fyi I only posted originally because I'd done the work and didn't want to 'waste' it.

we both used a array join string to check for presence of an element in the array. My approach was a little more confusing I guess because I sought to avoid the extra internal loop and you used it as a 'screen' for the requirement to perform the internal loop (I think).

re your comment: there may be other matches, but that didn't seem to interest you.

I guess we interpreted that differently..

I took this :
if there are matching numbers, take that number(s) and see if it exists anywhere within a third array (arrayC)

to mean:
as long as there is one match in the first check...to check each of those numbers for a match in the third array.

But english is not my strength even if it is my native language. And my actionscript's probably lagging significantly behind that. I've learnt a lot from your posts (you have a lot of them!)... and continue to do so.
kglad
Community Expert
Community Expert
June 13, 2007
you're welcome.
kglad
Community Expert
Community Expert
June 13, 2007
p.s. use the attach code option to display code.
Inspiring
June 13, 2007
@ kglad: I hadn't tried your code... yes it does return a match.. but only the first match as far as I can see.

@cjf3rules : I just realised my example code was wrong vs. your requirements

from the
//logic
comment onwards should be:
kglad
Community Expert
Community Expert
June 13, 2007
no, my code does what you want.
June 13, 2007
Oh, okay. Sorry kglad. I'm new to this array stuff.
I commented your code below. Am I correct with what each line does?
Can you explain what some of the lines do that I am confused about please?

//compare the two arrays and put into a new array?
function compareA(threes:Array, fives:Array):Array {
//put the threes array into a string and separate with commas?
var s1:String = threes.join(",");
// loop for as many times as there are items in the fives array
for (var i:Number = 0; i<fives.length; i++) {
//not sure what this does.
if (s1.indexOf(fives )>-1) {
//loop as many times as there are items in the threes array
for (var j:Number = 0; j<threes.length; j++) {
//e.g., if threes[1] equals fives[1]?
if (threes == fives
) {
//I have no idea what this return does.
return [j, i];
}
}
}
}
}
kglad
Community Expert
Community Expert
June 13, 2007
:

Inspiring
June 13, 2007
I'm sure it can be improved... I'm still learning myself. But I'll repost it with more comments so it *might* be easier to interpret what I was trying to do. The first function was not exactly necessary... you could use the second one to achieve the same thing. But I separated it out as it's a little simpler in terms of what its trying to achieve.
June 13, 2007
Thanks kglad!
But doesn't that function compare arrayA[1] to arrayB[1]? In other words, comparing the values based only one the same index order?
If so, that won't work for me. I need to compare ONLY that a number exists in both arrays, and NOT at any particular index order.
Inspiring
June 13, 2007
Here's another code snippet. I was a little slow.. kglad beat me to it.
June 13, 2007
Thanks GWD! I think that code would do the trick, but oh my! I think it will take me years to understand it all.
I'll try to disect it, but I think I got in over my head on this one.
kglad
Community Expert
Community Expert
June 13, 2007
the function below will return the indices of any matching array elements. if the return value is undefined, there are no matches: