Windows Vista Forums

Dictionary key and item manipulation
  1. #1


    Jo Winchester Guest

    Dictionary key and item manipulation

    I am trying to so something that I think should be quite simple....

    I have a script that needs to
    1. Read an Excel file
    2. Hold of of the information in memory - column A = server name, column B =
    web address
    3. Server names will not always be unique i.e. Server1, Server2 etc.
    4. Web addresses will always be unique
    5. Once information has been read and held in memory, it will then be used
    to run queries.

    I am trying to use a dictionary object, with the server names being the
    keys, and the web addresses being the items. I then want to be able to
    search the dictionary for all of the instances of a particular key i.e.
    server name, and enumerate their corresponding items i.e. web addresses.

    It can get it to work the other way round, by using this:
    Wscript.echo objDictionary.Item("WebAddress1")

    Any suggestions on how I may get this to work, or an alternative solution?





      My System SpecsSystem Spec

  2. #2


    Richard Mueller [MVP] Guest

    Re: Dictionary key and item manipulation

    Jo Winchester wrote:

    >I am trying to so something that I think should be quite simple....
    >
    > I have a script that needs to
    > 1. Read an Excel file
    > 2. Hold of of the information in memory - column A = server name, column B
    > =
    > web address
    > 3. Server names will not always be unique i.e. Server1, Server2 etc.
    > 4. Web addresses will always be unique
    > 5. Once information has been read and held in memory, it will then be used
    > to run queries.
    >
    > I am trying to use a dictionary object, with the server names being the
    > keys, and the web addresses being the items. I then want to be able to
    > search the dictionary for all of the instances of a particular key i.e.
    > server name, and enumerate their corresponding items i.e. web addresses.
    >
    > It can get it to work the other way round, by using this:
    > Wscript.echo objDictionary.Item("WebAddress1")
    >
    > Any suggestions on how I may get this to work, or an alternative solution?
    >
    >
    It makes sense to use the server name as the key, but you say this is not
    unique. The key values in a dictionary object must be unique. I think what
    you mean is that any server can have more than one web address. I think one
    solution is to append the web addresses, perhaps delimited with commas, in
    the items. You can use the Exists and Add methods of the dictionary objects.
    In brief, the code might be similar:
    =======
    Set objList = CreateObject("Scripting.Dictionary")
    objList.CompareMode = vbTextCompare

    ' Enumerate list of servers and web addresses somehow.
    Do ...
    strServer = <server name>
    strWebAddr = <web address>
    If (objList.Exists(strServer) = False) Then
    ' This is the first web address for this server.
    objList.Add strServer, strWebAddr
    Else
    ' This is an additional web address for this server.
    objList.Item(strServer) = objList.Item(strServer) & "," & strWebAddr
    End If
    Loop

    ' Display results.
    For Each strServer In objList.Keys
    Wscript.Echo "Server: " & strServer
    ' Convert comma delimited list of addresses into array.
    arrWebAddrs = Split(objList.Item(strServer), ",")
    ' Enumerate the array of web addresses for this server.
    For Each strWebAddr in arrWebAddrs
    Wscript.Echo "-- Web Address: " & strWebAddr
    Next
    Next

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

Dictionary key and item manipulation problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
A short one: Data dictionary item = array Leon VB Script 4 10 Mar 2010
rename-item, move-item and special chars. sardinian_guy PowerShell 3 26 Jul 2009
Copy-Item : Container cannot be copied onto existing leaf item. Steve PowerShell 3 17 Mar 2009
string manipulation karsagarwal VB Script 5 22 Nov 2008
Dictionary<TKey,TValue> as a Dictionary key in C# 2.0 dox .NET General 5 19 Mar 2008