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

Similar Threads
Thread Forum
A short one: Data dictionary item = array
Hi Is it possible to have an item as an array? Cheers
VB Script
rename-item, move-item and special chars.
Unluckily another problem has risen. :confused: Within my nested folders may occur that different files have the same name. c:\my_path\file.doc...
PowerShell
Copy-Item : Container cannot be copied onto existing leaf item.
I have a file rrr.config that contains the I run the script : $args = get-content -path c:\rrr.config #Make backups If ((select-string -path...
PowerShell
string manipulation
Hi, I am including a part of a code below with the output I got. Inn this code I read in 2 chars at a time from the "opt" string and divide the...
VB Script
Re: Outlook manipulation
On Nov 13, 2:07pm, "kjime...@xxxxxx" <kjime...@xxxxxx> wrote: This might get you started; $info = New-Object -ComObject Outlook.Application...
PowerShell
string manipulation
i have the following strings "Operation terminated unsuccessfully after 0.125 seconds." "Operation completed successfully in 358.719 seconds." ...
PowerShell
Dictionary<TKey,TValue> as a Dictionary key in C# 2.0
Hello, I'm having trouble using Dictionary classe. I want to use a Dictionary as a key for another Dictionary. The following code gives a sample...
.NET General