469,352 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Help with scripting dictionary ?

Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

for i = 1 To MyMsg.Count
Response.Write(MyMsg.Item(i))
next

What am I doing wrong here?

If I test with Response.Write(i) it will print :
1
2
3

Thanks in Advance
--

/ Sean the Mc /
"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
Jul 19 '05 #1
9 9721
Untested -

For Each k In MyMessage.Keys
Response.Write MyMessage(k) & "<br>"
Next

Bob Lehmann

"What-a-Tool" <Di************************@IHateSpam.Net> wrote in message
news:0s0Bc.3068$HN5.1345@lakeread06...
Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

for i = 1 To MyMsg.Count
Response.Write(MyMsg.Item(i))
next

What am I doing wrong here?

If I test with Response.Write(i) it will print :
1
2
3

Thanks in Advance
--

/ Sean the Mc /
"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)

Jul 19 '05 #2
"What-a-Tool" <Di************************@IHateSpam.Net> wrote in message
news:0s0Bc.3068$HN5.1345@lakeread06...

Nice spam trap.

: Dim MyMsg
: Set MyMsg = server.createObject("Scripting.Dictionary")
:
: MyMsg.Add "KeyVal1", "My Message1"
: MyMsg.Add "KeyVal2", "My Message2"
: MyMsg.Add "KeyVal3", "My Message3"
:
: for i = 1 To MyMsg.Count
: Response.Write(MyMsg.Item(i))
: next
:
: What am I doing wrong here?

Put the dictionary into a collection and then iterate the collection.

Dim MyMsg, myCol
Set MyMsg = CreateObject("Scripting.Dictionary") ' unless you're using TSQL,
just use CreateObject, not Server.CreateObject

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

myCol = MyMsg.Items ' Put dictionary into a collection

for i = 0 To MyMsg.Count - 1 ' Iterate collection
Response.Write(myCol(i)) ' to screen
next

Index starts at 0, not 1. You'll get a subscript out of range error if you
start with 1 and go to MyMsg.Count.

Result:
My Message1
My Message2
My Message3

HTH...

--
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
Jul 19 '05 #3
BLECH!

Seems like a lot of trouble when 3 lines of code can accomplish the same
thing.

Why put it in a collection when it *already_is* a collection?

Bob Lehmann

"Roland Hall" <nobody@nowhere> wrote in message
news:u7*************@tk2msftngp13.phx.gbl...
"What-a-Tool" <Di************************@IHateSpam.Net> wrote in message
news:0s0Bc.3068$HN5.1345@lakeread06...

Nice spam trap.

: Dim MyMsg
: Set MyMsg = server.createObject("Scripting.Dictionary")
:
: MyMsg.Add "KeyVal1", "My Message1"
: MyMsg.Add "KeyVal2", "My Message2"
: MyMsg.Add "KeyVal3", "My Message3"
:
: for i = 1 To MyMsg.Count
: Response.Write(MyMsg.Item(i))
: next
:
: What am I doing wrong here?

Put the dictionary into a collection and then iterate the collection.

Dim MyMsg, myCol
Set MyMsg = CreateObject("Scripting.Dictionary") ' unless you're using TSQL, just use CreateObject, not Server.CreateObject

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

myCol = MyMsg.Items ' Put dictionary into a collection

for i = 0 To MyMsg.Count - 1 ' Iterate collection
Response.Write(myCol(i)) ' to screen
next

Index starts at 0, not 1. You'll get a subscript out of range error if you start with 1 and go to MyMsg.Count.

Result:
My Message1
My Message2
My Message3

HTH...

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

Jul 19 '05 #4
"Bob Lehmann" <no****@dontbotherme.zzz> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
Untested -

For Each k In MyMessage.Keys
Response.Write MyMessage(k) & "<br>"
Next

Thank You--

/ Sean the Mc /
"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
Jul 19 '05 #5
"Bob Lehmann" wrote in message news:uF**************@TK2MSFTNGP09.phx.gbl...
: BLECH!
:
: Seems like a lot of trouble when 3 lines of code can accomplish the same
: thing.
:
: Why put it in a collection when it *already_is* a collection?

There's always a critic. Did you get any on ya'?

From the WSH docs...

Function DicDemo
Dim a, d, i, s ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR>" ' Create return string.
Next
DicDemo = s
End Function

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

Jul 19 '05 #6
heh - Right, MS documentation is well known for its tight, efficient coding
examples.

I suppose, then, that you would also reccommend this beauty taken from MSDN
Library...

Sub OpenMyDB()

Dim cnnNorthwind Dim rsCustomers
'Create the connection.
cnnNorthwind.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Office\Office\" & _
"Samples\Northwind.mdb;"

'Create recordset reference and set its properties.
Set rsCustomers = New ADODB.Recordset
rsCustomers.CursorType = adOpenKeyset
rsCustomers.LockType = adLockOptimistic

'Open recordset and print a test record.
rsCustomers.Open "Customers", cnnNorthwind, , , adCmdTable
Debug.Print rsCustomers.Fields(0).Value, rsCustomers.Fields(1).Value
rsCustomers.Close
cnnNorthwind.Close

End Sub

Bob Lehmann

"Roland Hall" <nobody@nowhere> wrote in message
news:OT*************@TK2MSFTNGP10.phx.gbl...
"Bob Lehmann" wrote in message news:uF**************@TK2MSFTNGP09.phx.gbl... : BLECH!
:
: Seems like a lot of trouble when 3 lines of code can accomplish the same
: thing.
:
: Why put it in a collection when it *already_is* a collection?

There's always a critic. Did you get any on ya'?

From the WSH docs...

Function DicDemo
Dim a, d, i, s ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR>" ' Create return string.
Next
DicDemo = s
End Function

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

Jul 19 '05 #7
"Bob Lehmann" wrote in message news:eY**************@TK2MSFTNGP10.phx.gbl...
: heh - Right, MS documentation is well known for its tight, efficient
coding
: examples.

Did I say that? I show you an example from the docs, which is what my post
followed, and now I'm making claims that MSFT docs examples are tight and
efficient coding? Is there a reason for this hostility?

: I suppose, then, that you would also reccommend this beauty taken from
MSDN
: Library...

<!--SNiP-->

Now you're just being ridiculous and off topic.

If you want to be specific and efficient, then your response should be
relative to what was asked. If you just want to be ridiculous and off
topic, we can do that too.

You're right. It IS untested and plugged into the OPs code would not work.
You'd get an object error.

Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

For Each k In MyMessage.Keys
Response.Write MyMessage(k) & "<br>"
Next

MyMessage is NOT a defined object.
It should actually be this:

Dim MyMsg, k
Set MyMsg = CreateObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

For Each k In MyMsg.Keys
Response.Write MyMsg(k) & "<br />"
Next

For someone who is crying for efficiency, one would think you'd test before
responding or at least use the same object especially when you're whining
over one line of code.

But, if we really wanted to be efficient, we'd make these all purpose
routines so they could be used again, wouldn't we? And, we'd probably
#include them but I'll leave that as an exercise.

Dim MyMsg, MyNextMsg
Set MyMsg = CreateObject("Scripting.Dictionary")
Set MyNextMsg = CreateObject("Scripting.Dictionary")

strMyMsg = "KeyVal1,My Message1,KeyVal2,My Message2,KeyVal3,My Message3"
strMyMsg2 = "KeyVal4,My Message4,KeyVal5,My Message5,KeyVal6,My Message6"
strMyNextMsg = "KeyVal1,Obladi,KeyVal2,Oblada,KeyVal3,Life goes on bra"

sub addItems(d, str)
dim m, i
m = split(str,",")
for i = 0 to ubound(m) step 2
d.Add m(i), m(i + 1)
next
end sub

sub showItems(d)
dim k
For Each k In d.Keys
Response.Write d(k) & "<br />"
Next
end sub

addItems MyMsg, strMyMsg
addItems MyMsg, strMyMsg2
showItems MyMsg
addItems MyNextMsg, strMyNextMsg
showItems MyNextMsg

set MyMsg = nothing
set MyNextMsg = nothing

or this...

Dim MyMsg, MyNextMsg
Set MyMsg = CreateObject("Scripting.Dictionary")
Set MyNextMsg = CreateObject("Scripting.Dictionary")

strMyMsg = Array("KeyVal1","My Message1","KeyVal2","My
Message2","KeyVal3","My Message3")
strMyMsg2 = Array("KeyVal4","My Message4","KeyVal5","My
Message5","KeyVal6","My Message6")
strMyNextMsg = Array("KeyVal1","Obladi","KeyVal2","Oblada","KeyVa l3","Life
goes on bra")

sub addItems(d, m)
dim i
for i = 0 to ubound(m) step 2
d.Add m(i), m(i + 1)
next
end sub

sub showItems(d)
dim k
For Each k In d.Keys
Response.Write d(k) & "<br />"
Next
end sub

addItems MyMsg, strMyMsg
addItems MyMsg, strMyMsg2
showItems MyMsg
addItems MyNextMsg, strMyNextMsg
showItems MyNextMsg

And if you change:

Response.Write d(k) & "<br />"

To:

WScript.Echo d(k)

It works in WSH.

It still may not be the most efficient but it's more modular and reusable.
If you're going to have a cow about efficiency, then please use the same
object the OP posted otherwise it just looks like misplaced anger.

--
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
Jul 19 '05 #8
OK. I mistyped the variablename the OP used. EXCUUUUUUUUUUUSSSSSEEEEEE ME!

I think my disclaimer about it being untested was sufficient CYA. Also, the
OP seems satisfied with my answer.

You rationalized your answer by pointing out that you got it from the MS
Docs, implying that since MS was the source, it must be good. My response
refuted your rationale - that's all. I fail to see the hostility in that.

Apparently you work alone (which would be a good thing, since you seem to
have an ultra-high sensitivity level), or have never participated in a code
review where programmers' code is routinely challenged as a matter of
course.

As for being ridiculous.....
<hostility type="deserved">
This is a pretty f*cking funny response given your sig there, MR. "without
even,,, the implied fitness for a particular purpose". Did you spend an hour
in a froth typing up all that other crap? Is your pay based on the number of
lines in your code?
</hostility>

Bob Lehmann

"Roland Hall" <nobody@nowhere> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
"Bob Lehmann" wrote in message news:eY**************@TK2MSFTNGP10.phx.gbl... : heh - Right, MS documentation is well known for its tight, efficient
coding
: examples.

Did I say that? I show you an example from the docs, which is what my post followed, and now I'm making claims that MSFT docs examples are tight and
efficient coding? Is there a reason for this hostility?

: I suppose, then, that you would also reccommend this beauty taken from
MSDN
: Library...

<!--SNiP-->

Now you're just being ridiculous and off topic.

If you want to be specific and efficient, then your response should be
relative to what was asked. If you just want to be ridiculous and off
topic, we can do that too.

You're right. It IS untested and plugged into the OPs code would not work. You'd get an object error.

Dim MyMsg
Set MyMsg = server.createObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

For Each k In MyMessage.Keys
Response.Write MyMessage(k) & "<br>"
Next

MyMessage is NOT a defined object.
It should actually be this:

Dim MyMsg, k
Set MyMsg = CreateObject("Scripting.Dictionary")

MyMsg.Add "KeyVal1", "My Message1"
MyMsg.Add "KeyVal2", "My Message2"
MyMsg.Add "KeyVal3", "My Message3"

For Each k In MyMsg.Keys
Response.Write MyMsg(k) & "<br />"
Next

For someone who is crying for efficiency, one would think you'd test before responding or at least use the same object especially when you're whining
over one line of code.

But, if we really wanted to be efficient, we'd make these all purpose
routines so they could be used again, wouldn't we? And, we'd probably
#include them but I'll leave that as an exercise.

Dim MyMsg, MyNextMsg
Set MyMsg = CreateObject("Scripting.Dictionary")
Set MyNextMsg = CreateObject("Scripting.Dictionary")

strMyMsg = "KeyVal1,My Message1,KeyVal2,My Message2,KeyVal3,My Message3"
strMyMsg2 = "KeyVal4,My Message4,KeyVal5,My Message5,KeyVal6,My Message6"
strMyNextMsg = "KeyVal1,Obladi,KeyVal2,Oblada,KeyVal3,Life goes on bra"

sub addItems(d, str)
dim m, i
m = split(str,",")
for i = 0 to ubound(m) step 2
d.Add m(i), m(i + 1)
next
end sub

sub showItems(d)
dim k
For Each k In d.Keys
Response.Write d(k) & "<br />"
Next
end sub

addItems MyMsg, strMyMsg
addItems MyMsg, strMyMsg2
showItems MyMsg
addItems MyNextMsg, strMyNextMsg
showItems MyNextMsg

set MyMsg = nothing
set MyNextMsg = nothing

or this...

Dim MyMsg, MyNextMsg
Set MyMsg = CreateObject("Scripting.Dictionary")
Set MyNextMsg = CreateObject("Scripting.Dictionary")

strMyMsg = Array("KeyVal1","My Message1","KeyVal2","My
Message2","KeyVal3","My Message3")
strMyMsg2 = Array("KeyVal4","My Message4","KeyVal5","My
Message5","KeyVal6","My Message6")
strMyNextMsg = Array("KeyVal1","Obladi","KeyVal2","Oblada","KeyVa l3","Life
goes on bra")

sub addItems(d, m)
dim i
for i = 0 to ubound(m) step 2
d.Add m(i), m(i + 1)
next
end sub

sub showItems(d)
dim k
For Each k In d.Keys
Response.Write d(k) & "<br />"
Next
end sub

addItems MyMsg, strMyMsg
addItems MyMsg, strMyMsg2
showItems MyMsg
addItems MyNextMsg, strMyNextMsg
showItems MyNextMsg

And if you change:

Response.Write d(k) & "<br />"

To:

WScript.Echo d(k)

It works in WSH.

It still may not be the most efficient but it's more modular and reusable.
If you're going to have a cow about efficiency, then please use the same
object the OP posted otherwise it just looks like misplaced anger.

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

Jul 19 '05 #9
"Bob Lehmann" wrote in message news:Ox**************@TK2MSFTNGP09.phx.gbl...
: OK. I mistyped the variablename the OP used. EXCUUUUUUUUUUUSSSSSEEEEEE ME!

I wasn't going to say a thing about your post until you so elegantly spewed
BLECH and started bitching about efficiency. Perhaps if you tested your
code...

: I think my disclaimer about it being untested was sufficient CYA. Also,
the
: OP seems satisfied with my answer.

I'm sure he's competent enough to figure out the different object name.

: You rationalized your answer by pointing out that you got it from the MS
: Docs, implying that since MS was the source, it must be good.

Nope. You infered it to mean that. It was as I said it was. I posted I
had followed an example from MSFT.

: My response
: refuted your rationale - that's all. I fail to see the hostility in that.

Oh, it could be my error. I didn't know BLECH meant refuting one's
rationale. Your response showed you are incapable of rendering corrective
criticism in a professional manner.

: Apparently you work alone (which would be a good thing, since you seem to
: have an ultra-high sensitivity level), or have never participated in a
code
: review where programmers' code is routinely challenged as a matter of
: course.

Is this more professional rationale? Is it normal for you to BLECH all over
your co-workers code? You know Bob, this is not a contest. It was my
understanding we were all here to help people and gain knowledge. Perhaps
you have a different agenda?

: As for being ridiculous.....
: <hostility type="deserved">
: This is a pretty f*cking funny response given your sig there, MR. "without
: even,,, the implied fitness for a particular purpose".

http://www.m-w.com/cgi-bin/dictionar...&va=disclaimer

Perhaps you need a definition for hypocrite too? Isn't this the same?
[: I think my disclaimer about it being untested was sufficient CYA. Also,
the
: OP seems satisfied with my answer.]

: Did you spend an hour
: in a froth typing up all that other crap?

Actually I type quite fast. Is this more professional criticism?

Is your pay based on the number of
: lines in your code?
: </hostility>

I'm not paid to be in the NGs. I post to people to try to help, which may
or may not be the best example they receive. I don't claim to be the best
programmer or have the most knowledge of any of the languages I write in.
It was my understanding this was a community where people can discuss ideas
and offer help to those that request it and gain knowledge by review of
other examples.

I really don't have any idea why you think there is a benefit in vexing
someone just because you disagree with an answer they gave to someone else.

--
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
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gram | last post: by
8 posts views Thread by Howard Kaikow | last post: by
3 posts views Thread by jaffar.kazi | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.