The VBScript Network and Systems Administrator's Cafe

Jul 10 2008   3:23AM GMT

How to use the VBScript Scripting.Dictionary object to store data to make key decisions.



Posted by: Jerry Lees
Tags:
Random Numbers
randomize
rnd
Scripting.Dictionary
VBScript
VBScript Functions
VBScript Objects

I’ve been writing lately on individual topics and thought it was time for a little fun, while introducing you to a new object in the process… the Scripting.Dictionary object! (and random number generation in the process)

The Scripting.Dictionary object is a quite useful object. It will let you store data in a categorized manner through it’s use of a key when you add data to the object. It works very much like a multi-dimensional array would, except that it is much easier to retrieve a specific type of data.

You place the data into the object via the .add method and retrieve a specific item via it’s key. The key can either be numeric or a string. In the example below, I’ve chosen a number. Now for the example… a script so useful that I’m certain it will become a key part of your decision making process at work… The Magic Eight Ball! Now, before you laugh not everyone has a magic eight ball (I have two) and that’s a shame because they really do help with making decisions.

Just run the script below several times and ask it these questions:

Will anyone notice if I reboot the exchange server now?
Will the network survive while the Network Guys upgrade the switches?
Will the servers survive another patch Tuesday?
Does my boss appreciate me enough to give me a $10,000 raise?

And now for the example code you’ve been waiting for…

 Option explicit
Dim objDictionary
Set objDictionary = CreateObject(“Scripting.Dictionary”)

objDictionary.Add 1, “All signs point to NO”
objDictionary.Add 2, “Outlook not likely”
objDictionary.Add 3, “All signs point to YES”
objDictionary.Add 4, “Maybe”
objDictionary.Add 5, “You want what… When?”
objDictionary.Add 6, “Definitely”
objDictionary.Add 7, “Reply Hazy, try again later”
objDictionary.Add 8, “I could tell you, but I’d have to kill you”
objDictionary.Add 9, “Sure, why not.”
objDictionary.Add 10, “One word Answer. Snowball… you figure it out.”

Dim Answer
Randomize

Do
 Answer = int(Rnd(1)*10)
Loop While (Answer < 1)
WScript.Echo objDictionary.Item(Answer)

As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: