1 Reply Latest reply on Nov 11, 2009 12:49 AM by Ratsnackbar

    Comparing Arrays

    MrFork Level 1

      Hello Guys, I need some help with some logic.

       

      What I have is two arrayCollections containing some data.

       

      Array1 = ({link:'home.php},

      {link:'contactus.php'},

      {link:'hello.php'},

       

      Array2 = ({pagepath:'home.php, merchant: 'Currys'}

      {pagepath:'contactus.php, merchant: 'PCWorld'}

      {pagepath:'hello.php, merchant: 'Boots'},

       

      What I want is

       

      if (Array1.link == Array2.pagepath)

       

      // Add merchant from Array2 to Array1.

       

      Can anyone help?

        • 1. Re: Comparing Arrays
          Ratsnackbar Level 2

          Remember, data in arrays are accquired through their index.  So to get the first link in Array1 you would have to use something like:

           

          value:String = Array1[0].link

           

          To perform your compair you can loop through the first array and use the loop index to qualify the array index.  The problem however starts when you consider that your second array may not always have the same number of objects or the objects might not be in the ideal order.

           

          For example:

           

            Array1 = ({link:'home.php},

            {link:'hello.php'},

            {link:'contactus.php'},   //index is 2 in this array

           

            Array2 = ({pagepath:'home.php, merchant: 'Currys'}

            {pagepath:'contactus.php, merchant: 'PCWorld'},  //index is 1 in this array

            {pagepath:'hello.php, merchant: 'Boots'},

           

          So if you are going to do an exact value compair of the link and pagepath in each of the arrays you would have to start by taking the value of Array[0].link and compair it to every object in array2 and then move onto the next.  You could accomplish this with a nested loop.  I guess the question would be is there a more effecient way to do this.

           

          instead you may want to consider using a dictionary object for to replace Array1.  You could use the Links value as the Key for each Dictionary item and the merchant value as the value of the item.  You can then loop through array2 and use a lookup to see if the pagepath is available as a key in the DictionaryObject.  If it is set it's value to that of the merchant.  If not create the new key and set it's value.

           

          Hope that helps stir up good ideas even if it does not entirely solve your problem.

           

          -Joe