A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: compare array

  1. #1
    Junior Member
    Join Date
    Dec 2000
    Posts
    1
    Can I compare array like these?
    I have an array like this,

    myArray=new Array(a,f,s,h,b);

    and another two dimension arrays,

    allArray=new Array();
    allArray[0}=new Array(s,r,a,w,g);
    allArray[1}=new Array(l,r,e,x,m);
    allArray[2}=new Array(h,f,b,s,a);
    allArray[3}=new Array(a,v,o,m,n);
    allArray[4}=new Array(z,d,q,b,c);

    How can I compare myArray and allArray and find the one from the allArray list which is match to myArray? But it doesn't matter of the order. In this case, it should be found out myArray matches allArray[2].


    Can anybody help me?

  2. #2
    Senior Member
    Join Date
    Apr 2001
    Location
    Sydney, Australia
    Posts
    271
    G'Day,

    I'm not to sure about how to do it in flash but I can help you with the algorithm.

    Basically you need a nested for loop with an if statement inside that. Sould be something like...

    for (int i = 0; i < allArray[i].length - 1; i++)
    {
    for (int x = 0; x < myArray.length - 1; x++)
    {
    if (myArray[x] == allArray[i][x])
    {
    count += 1;
    }
    if (count == myArray.length)
    {
    ans = i;
    }
    count = 0;
    }
    }

    This will return the element number of the array which matches myArray. See how that goes, as I said I don't expect the syntax to be right for flash but I know the logic is.

  3. #3
    Senior Member
    Join Date
    Apr 2001
    Location
    Sydney, Australia
    Posts
    271
    G'Day,

    Sorry big booboo. You need another for loop in there, let me try again

    for (int i = 0; i < allArray.length - 1; i++)
    {
    for (int x = 0; x < myArray.length - 1; x++)
    {
    for (int p = 0; p < allArray[i].length - 1; p++)
    {
    if (myArray[x] == allArray[i][p])
    {
    count += 1;
    }
    if (count == myArray.length)
    {
    ans = i;
    }
    }
    count = 0;
    }
    }

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Posts
    2,235
    Here's something I just done in Javascript:

    myArray=new Array("a","f","s","h","b");
    allArray=new Array();
    allArray[0]=new Array("s","r","a","w","g");
    allArray[1]=new Array("l","r","e","x","m");
    allArray[2]=new Array("h","f","b","s","a");
    allArray[3]=new Array("a","v","o","m","n");
    allArray[4]=new Array("z","d","q","b","c");

    counter=0
    temp=""
    lgn=myArray.length
    function checkA(){
    for(j=0;j<lgn;++j){
    for(k=0;k<lgn;++k){
    for(l=0;l<lgn;++l){
    if(myArray[k]==allArray[j][l]){
    counter+=1;
    temp+=myArray[k];
    }
    }
    }
    document.write(temp+"-"+counter+"<br>")
    counter=0
    temp=""
    }
    }

    The syntax is almost identical to flash and should be easily adaptable...


    ~mgb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center