Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loop through dictionary object

3,028 views
Skip to first unread message

Jo Winchester

unread,
Feb 16, 2005, 10:51:03 AM2/16/05
to
I am writing a script that reads and test file, and then creates a dictionary
based on set criteria. Each key that is added to the dictionary is appended
with a number. This bit works OK. What I then need to do is output the data
held in the dictionary, based on the first letter of the dictionary key. I
need to do some sort of loop through the dictionary, that will look for all
intances starting with "1", then all starting with "2", until it reaches the
end of the dictionary.

This piece of script will echo each key starting with "1", but I then need
it to loop and show "2" etc etc. I have tried adding a Loop function, with
an incremental counter i.e. n=0, n=n+1, but it's not quite doing what I need.

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"

colKeys = objDictionary.Keys
For Each strKey in colKeys
if Left (strKey,1) = n then Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
Next

colItems = objDictionary.Items
For Each strItem in colItems
if Left (strItem,1) = "1" then Wscript.Echo strItem
Next

Jo Winchester

unread,
Feb 16, 2005, 10:55:01 AM2/16/05
to
Script re-posted with slight amendment

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"

colKeys = objDictionary.Keys
For Each strKey in colKeys

if Left (strKey,1) = "1" then Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
Next

colItems = objDictionary.Items
For Each strItem in colItems
if Left (strItem,1) = "1" then Wscript.Echo strItem
Next

Agoston Bejo

unread,
Feb 16, 2005, 11:03:29 AM2/16/05
to

"Jo Winchester" <JoWinc...@discussions.microsoft.com> wrote in message
news:515A1B1D-D0F6-4B31...@microsoft.com...

> I am writing a script that reads and test file, and then creates a
dictionary
> based on set criteria. Each key that is added to the dictionary is
appended
> with a number. This bit works OK. What I then need to do is output the
data
> held in the dictionary, based on the first letter of the dictionary key.
I
> need to do some sort of loop through the dictionary, that will look for
all
> intances starting with "1", then all starting with "2", until it reaches
the
> end of the dictionary.
>
> This piece of script will echo each key starting with "1", but I then need
> it to loop and show "2" etc etc. I have tried adding a Loop function,
with
> an incremental counter i.e. n=0, n=n+1, but it's not quite doing what I
need.
>
> Set objDictionary = CreateObject("Scripting.Dictionary")
> Set objDictionary = CreateObject("Scripting.Dictionary")
> objDictionary.Add "1_file.txt", "1"
> objDictionary.Add "2_file.txt", "2"
> objDictionary.Add "3_file.txt", "3"
>

Sub EnumerateKeysStartingWith(n) 'see below
Dim colKeys
'Your code:


colKeys = objDictionary.Keys
For Each strKey in colKeys
if Left (strKey,1) = n then Wscript.Echo
mid(strkey,InStr(strKey,"_")+1)
Next

End Sub

Dim n : n = 0
Do While EnumerateKeysStartingWith(n)
Loop


You said you only wanted to examine keys, so I skipped this part:

Agoston Bejo

unread,
Feb 16, 2005, 11:09:20 AM2/16/05
to

"Jo Winchester" <JoWinc...@discussions.microsoft.com> wrote in message
news:7E6FDE41-2E41-4CD6...@microsoft.com...

> Script re-posted with slight amendment
>
> Set objDictionary = CreateObject("Scripting.Dictionary")
> Set objDictionary = CreateObject("Scripting.Dictionary")
> objDictionary.Add "1_file.txt", "1"
> objDictionary.Add "2_file.txt", "2"
> objDictionary.Add "3_file.txt", "3"

Based on your code (absolutely untested, though, but the solution should
look something like this):


'---------------------------

Function EnumerateKeysStartingWith(n) 'Returns Boolean
'returns if there was at least one key starting with n

Dim colKeys, bKeyExist
bKeyExist = False

colKeys = objDictionary.Keys
For Each strKey in colKeys

If Left (strKey,1) = "1" then
Wscript.Echo mid(strkey,InStr(strKey,"_")+1)
bKeyExist = True
End If
Next
EnumerateKeysStartingWith = bKeyExist

End Function 'EnumerateKeysStartingWith

Dim n : n = 1
Do While EnumerateKeysStartingWith(n)
Loop

'----------------------------

Jo Winchester

unread,
Feb 16, 2005, 11:37:06 AM2/16/05
to
Thanks for you quick replies - it's looking promising. Have you posted the
script in one post and the function in another? Any chance that you could
post the whole script in one go?
I have tried your script, but it is just looping and returning the first
file name in the list in an endless loop. Any suggestions?
Thanks again for your help - it's really appreciated.

Roland Hall

unread,
Feb 16, 2005, 12:48:42 PM2/16/05
to
"Jo Winchester" wrote in message
news:515A1B1D-D0F6-4B31...@microsoft.com...
:I am writing a script that reads and test file, and then creates a

A slightly different approach...

dim strKey, colKeys, colItems, strArr


Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "1_file.txt", "1"
objDictionary.Add "2_file.txt", "2"
objDictionary.Add "3_file.txt", "3"

colKeys = objDictionary.Keys
colItems = objDictionary.Items

For Each strKey in colKeys
strArr = split(strKey,"_")
Wscript.Echo colItems(strArr(0)-1) & ". " & strArr(1)
next

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp


Jo Winchester

unread,
Feb 16, 2005, 1:59:02 PM2/16/05
to
Thanks for your post, but it's still not quite doing what I need.

The output from this script will eventually make up the body of an alerting
e-mail. What I need the script to do is write a line:
"Files Transfered from Server A to Server B"
And then list all of the file names starting with "1", then write another
line,
Files Transfered from Server C to Server D"
And then list all of the file names starting with "2" etc etc.
I'm happy that I can do all of this - apart from reading the file names from
the dictionary in an incremental way. I have been experimenting with n=1,
n=n+1 with a loop, but it is only recognising the initial n=1. Even though I
can echo n with a value of 2, it is not picking up the dictionary items that
relate to it.

McKirahan

unread,
Feb 16, 2005, 6:01:28 PM2/16/05
to
"Jo Winchester" <JoWinc...@discussions.microsoft.com> wrote in message
news:515A1B1D-D0F6-4B31...@microsoft.com...

Will this help?

Sorting a Scripting Dictionary Populated with String Data
http://support.microsoft.com/support/kb/articles/Q246/0/67.ASP

Roland Hall

unread,
Feb 17, 2005, 12:10:58 AM2/17/05
to
"Jo Winchester" wrote in message
news:95D58446-A2C3-4893...@microsoft.com...
:

: Thanks for your post, but it's still not quite doing what I need.


:
: The output from this script will eventually make up the body of an
alerting
: e-mail. What I need the script to do is write a line:

: "Files Transferred from Server A to Server B"


: And then list all of the file names starting with "1", then write another
: line,
: Files Transfered from Server C to Server D"
: And then list all of the file names starting with "2" etc etc.
: I'm happy that I can do all of this - apart from reading the file names
from
: the dictionary in an incremental way. I have been experimenting with n=1,
: n=n+1 with a loop, but it is only recognising the initial n=1. Even
though I
: can echo n with a value of 2, it is not picking up the dictionary items
that
: relate to it.

Joe...

First, the conversation is to be read from top to bottom.
Second, if you're going to change the rules in the middle of the game, I'm
going to get more confused. This is the first I have read about "Files
transferred from Server A to Server B", etc. There is no connectivity in
this script to any remote server.

You are using a dictionary which appears you do not need to do. You have
filenames with begin with numbers, used as keys, that match item numbers.
That's redundant.

Perhaps you should state your FULL goal and someone can direct you to where
you need to be and then perhaps you can obtain help with your scripting.

Currently I have no way to associate servers and filenames with preceding
numbers.

Agoston Bejo

unread,
Feb 17, 2005, 4:08:15 AM2/17/05
to
Sorry, the one with a 'Sub' was posted by accident. The other one with the
'Function' is what I actually wanted to post. Ignore the former one, please.


"Jo Winchester" <JoWinc...@discussions.microsoft.com> wrote in message

news:A25184A1-0F0E-4F2F...@microsoft.com...

0 new messages