• Collection, array or dictionary?

    Author
    Topic
    #466463

    I looping in text file “line by line”.
    During the loop i get a part of string line and store it in My_Var (my_var is dimensioned as string).
    Now, i want to save all values of my_var in an array or in a colection or in a dictionary object.

    I want to use this way to controll duplicate value of my_var.
    Exaple:

    my_var=”sss” controll in contaier and if not is present store this svalue (in an array or in a colection or in a dictionary object)
    no action
    my_var=”aaa” controll in contaier and if not is present store this svalue (in an array or in a colection or in a dictionary object)
    no action
    my_var=”sss” no store in container becuase is alreday present in container
    action msgbox(“just present”)

    ecc…
    …..

    How to?
    Suggest me via a littele code the fast way to controll my_var is just processed.

    Viewing 1 reply thread
    Author
    Replies
    • #1208348

      Assuming your data doesn’t have commas in it (you can use another character, maybe hash) I would add the new value to my_var after a string search.

      Code:
      if instr(my_var, newvalue) = 0 then 'if found new value do not add to my_var
        my_var = my_var & "," & newvalue
      end if
      

      You end up with my_var containing all results in a comma separated string.

      cheers, Paul

    • #1208364

      Here’s a sample using a Dictionary. Each member of a Dictionary has two components, a Key and an Item, but you don’t need both, so this sample just stores the strings in the Keys (and sets the Item value of each to 0).

      In case you want more information, here’s a post I did a while ago about Dictionaries vs. Collections. The “Exists” method is one of the Dictionary’s nicer features.

      Code:
          Dim dctX As New Scripting.Dictionary
          Dim varX As Variant
      
          dctX.Add "apple", 0
          dctX.Add "orange", 0
      
          If dctX.Exists("banana") Then
              MsgBox "Banana's there already."
           Else
              dctX.Add "banana", 0
          End If
      
          If dctX.Exists("apple") Then
              MsgBox "Apple's there already."
           Else
              dctX.Add "apple", 0
          End If
      
          For Each varX In dctX
              Debug.Print varX
          Next varX
      
          Set dctX = Nothing
      
    Viewing 1 reply thread
    Reply To: Collection, array or dictionary?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: