Help with scripting dictionary ?

W

What-a-Tool

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

Bob Lehmann

Untested -

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

Bob Lehmann
 
R

Roland Hall

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
 
B

Bob Lehmann

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
 
W

What-a-Tool

Bob Lehmann said:
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)
 
R

Roland Hall

in message : 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
 
B

Bob Lehmann

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
 
R

Roland Hall

in message : 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","KeyVal3","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
 
B

Bob Lehmann

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
 
R

Roland Hall

in message : 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/dictionary?book=Dictionary&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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top