View Full Version : scripting.Dictionary
aabruzzese
09-27-2002, 03:52 PM
Hi guys,
I am trying to understand scriptiong dictionary objects.
Is the object made up of a key and item only ?
Is it possible to create a dictionary where the item contains
more then one value.
ie:
Set fleet = Server.CreateObject("Scripting.Dictionary")
fleet.Add Name, someattribute
In this case my understanding is that Name is the Key and
someattribute is the item.
Is it possible to have an item that is an array of attributes?
Lets take one instance -
key Enterprise
someattribute (speed,range,armor,gunnery)
is it possible to do this with a dictionary item ?
I have searched high and wide and have not found any samples or doco show one key have an item with more then one value.
Thanks
Angelo
aabruzzese
09-29-2002, 04:31 PM
Hi Guys,
Ok I am getting closer to solving this baby.
One problem I am having is to retreive the array from the item associated to a key in the dictionary.
Here is some of the code.
<%
'*******************************************************
'* Subroutines that are called by the runtime script *
'*******************************************************
'
Sub loadFleet()
' Declare Local Variables
Dim oConn ' Database connection string
Dim oRS ' ADOBD recordset
Dim sSQL ' Sql command string
Dim StrImgPath ' path to images
Dim Name ' Ship Name
Dim AirStrike ' Ship AirStrike factor
Dim Gunnery ' Ship Gunnery factor
Dim Patrol ' Ship Image Icon path
Dim TOA ' Ship Turn of arrival
Dim Speed ' Ships Speed factor
Dim Armor ' Ships Armor factor
' Initialization
StrImgPath = "./images/vitp/"
'StrImgPath = "/abscanet/img/ijnpt/"
' Create an ADO Connection to connect to the database.
Set oConn = Server.CreateObject("ADODB.Connection")
'Database Connection String includes path to database
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & "c:\vitp\ABSCANET1.mdb")
'oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\abscanet\db\ABSCANET.mdb"))
' Execute a query using the connection object. It automatically
' creates and returns a recordset which we store in our oRS variable.
sSQL = "SELECT Name, Patrol, TOA, AirStrike, Attack, Speed, Armor FROM IJNSHIPS"
sSQL = sSQL & " ORDER BY TOA Asc,AirStrike Desc, Attack Desc, Name Asc,Armor Desc"
Set oRS = oConn.Execute(sSQL)
' Loop through the recordset adding each ship to the
' dFleet dictionary use MoveNext to read the next record,
' stopping when oRS.EOF is reached.
Dim fleet
Dim i, Ship, obKey, thisitem, thiskey, shipParams
Do While Not oRS.EOF
i = i + 1
Name = oRS.FIELDS("Name")
AirStrike = oRS.FIELDS("AirStrike")
Gunnery = oRS.FIELDS("Attack")
Patrol = StrImgPath & oRS.FIELDS("Patrol")
TOA = oRS.FIELDS("TOA")
Speed = oRS.FIELDS("Speed")
Armor = oRS.FIELDS("Armor")
shipParams = Array Name,AirStrike,Gunnery,Patrol,TOA,Speed,Armor)
dFleet.Add i, shipParams
oRS.MoveNext
Loop
' Close the recordset and database connection and dispose of them.
oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
' End If
End Sub
'*******************************************************
'* *
'* showFleet *
'* This subroutin shos the available fleet *
'* *
'*******************************************************
Sub showdFleet()
Dim fkey
Dim fitem
%>
<font size=4>Available Fleet</font>
<TABLE Border=1 CellPadding=1 CellSpacing=1 width="127">
<TR>
<TD>Unit</TD>
<TD>Melee</TD>
</TR>
<%
fkeys = dFleet.Keys
fitems = dFleet.Items
For i = 0 to dFleet.Count - 1
fkey = fkeys(i)
fitem = fitems(i)
response.write fitem & "This is the item array <br>"
%>
<TR>
<TD width="46"><img width=42 height=42 border=0 vspace=0 SRC="<%= fitem%>"><br><%= fitem%></TD>
<TD ALIGN=Left valign=Bottom width="65"><A HREF="./firemission5.asp?action=AddShip&Ship=<%= fkey %>">Add</A></TD>
</TR>
<%
Next
%>
</TABLE>
<%
End Sub
I am unable to extract the items associated to the key, I keep getting subscript out of range or type mismatch.
I have tried various ways to extract it, but all have failed.
How do you code something to pull that array out and split it ?
I mean if it can store it no problem, why should it be so difficult to extract it and split it.
Angelo
Alekz
09-30-2002, 08:41 AM
Hi,
You do not need to assign an array to Dictionary item value as a delimited string. You can create an ordinary Array and assign ot. Actually You can assign virtually anything to a dictionary item, even another dictionary Object - this is sometimes used to create tree structures...
Alex
whammy
09-30-2002, 10:31 AM
Ok, after rereading your post, I think I was smoking crack last night or something, anyway this is what you need to study:
http://www.w3schools.com/asp/asp_ref_dictionary.asp
aabruzzese
09-30-2002, 12:33 PM
Hey guys,
Thanks for all the help, I actually figured it out after a little
walk. I mean nothing like turning the old computer off and
stepping away from a problem to help you think and
solve issues at hand.
Anyways I was able to figure the code I needed to extract
an item value that was an array in of itself.
Actually the solution does not require spliting an array at
all, more like get current item and pulling the value
of each element using the CStr or CInt into a variable.
Thanks for all the help.
Angelo
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.