• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Dictionary key and item manipulation

J

Jo Winchester

#1
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?
 
R

Richard Mueller [MVP]

#2
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
--