arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    arrays

    I have a collection of objects that may have duplicates in it. What I
    want to do is to iterate through the list and only add the non
    duplicates to an array, so as I am adding to the array, I need some way
    of checking against the current contents of the array. Is this
    possible, or is there a better way to do this?



    *** Sent via Developersdex http://www.developersdex.com ***
  • Jon Paal [MSMD]

    #2
    Re: arrays

    you could use a dictionary and the 'exists' method

    The following examples illustrate the use of the Exists method.

    [JScript]
    function keyExists(k)
    {
    var fso, s = "";
    d = new ActiveXObject(" Scripting.Dicti onary");
    d.Add("a", "Athens");
    d.Add("b", "Belgrade") ;
    d.Add("c", "Cairo");
    if (d.Exists(k))
    s += "Specified key exists.";
    else
    s += "Specified key doesn't exist.";
    return(s);
    }[VBScript]
    Function KeyExistsDemo
    Dim d, msg ' Create some variables.
    Set d = CreateObject("S cripting.Dictio nary")
    d.Add "a", "Athens" ' Add some keys and items.
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"
    If d.Exists("c") Then
    msg = "Specified key exists."
    Else
    msg = "Specified key doesn't exist."
    End If
    KeyExistsDemo = msg
    End Function


    Comment

    • Bob Barrows [MVP]

      #3
      Re: arrays

      Mike P wrote:
      I have a collection of objects that may have duplicates in it. What I
      want to do is to iterate through the list and only add the non
      duplicates to an array, so as I am adding to the array, I need some
      way of checking against the current contents of the array. Is this
      possible, or is there a better way to do this?
      >
      >
      >
      There's no shortcut. You need to do a nested loop.


      start loop through collection
      within the collection loop, loop through the array checking to see if object
      already exists.
      if not, add the object to the array and move to the next object

      Do you need more detail than that? if so, we sill need more details.

      --
      Microsoft MVP - ASP/ASP.NET
      Please reply to the newsgroup. This email account is my spam trap so I
      don't check it very often. If you must reply off-line, then remove the
      "NO SPAM"


      Comment

      Working...