Results 1 to 3 of 3
  1. #1
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,164
    Thanks
    47
    Thanked 976 Times in 906 Posts
    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

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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

Posting Permissions

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