Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ VBscript ➜ Emulating an array using MUSHclient variables.

Emulating an array using MUSHclient variables.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Magnum   Canada  (580 posts)  Bio
Date Fri 28 Dec 2001 12:07 PM (UTC)
Message
This thread is a continuation of my previous thread about LBound, UBound and ReDim. Along the way, I discovered that MUSHclient variables could not be array-type variables.

I wrote the following script to emulate an array using MUSHclient variables:

Sub MCSetArray (ArrayName, ArraySize, TheValue)
Dim arrPointers
Dim x
arrPointers = Split(ArraySize, ",")
For x = LBound(arrPointers) to UBound(arrPointers)
ArrayName = ArrayName & "_" & CStr(arrPointers(x))
Next
World.SetVariable ArrayName, TheValue
End Sub

Function MCGetArray (ArrayName, ArraySize)
Dim arrPointers
Dim x
arrPointers = Split(ArraySize, ",")
For x = LBound(arrPointers) to UBound(arrPointers)
ArrayName = ArrayName & "_" & CStr(arrPointers(x))
Next
MCGetArray = World.GetVariable(ArrayName)
End Function

Sub MCDelArray (ArrayName)
Dim VariableList
Dim StringPos
Dim x
VariableList = World.GetVariableList
ArrayName = ArrayName & "_"
arrPointers = Split(ArraySize, ",")
For x = LBound(VariableList) to UBound(VariableList)
StringPos = InStr(1, VariableList(x), ArrayName, vbTextCompare)
If StringPos = 1 Then
World.DeleteVariable VariableList(x)
End If
Next
End Sub

The script is not fool-proof, so it could use a little bit of work if you want to make it 'safer'.

If you want to work with a muultidimensional array, you should pass the size arguments together, seperated by a comma [,] without any SPACE characters. Also, send the size argument as a string.

The MCDelArray works my grabbing the list of variables, and deleting all that start with your arrayname followed by the underscore [_] character. You should be aware of that when creating variable names. For example, if you create an array with the name "arrNames", and also an array with the name "arrNames_Friends", BOTH arrays will be deleted if you make a call to MCDelArray with "arrNames" as the argument.

Here is another pile of script that I created to test the array emulation script:

Sub DTest
MCDelArray "arrSpellsPointer"
MCDelArray "arrSpells"
World.Note "Arrays Deleted."
End Sub

Sub ITest
Dim x, y
World.SetVariable "arrSpellsPointer_LB", 1
World.SetVariable "arrSpellsPointer_UB", 5
World.SetVariable "arrSpells_LB1", 1
World.SetVariable "arrSpells_UB1", 6
World.SetVariable "arrSpells_LB2", 1
World.SetVariable "arrSpells_UB2", 5
For x = World.GetVariable("arrSpellsPointer_LB") to World.GetVariable("arrSpellsPointer_UB")
MCSetArray "arrSpellsPointer", CStr(x), Empty
Next
For x = World.GetVariable("arrSpells_LB1") to World.GetVariable("arrSpells_UB1")
For y = World.GetVariable("arrSpells_LB2") to World.GetVariable("arrSpells_UB2")
MCSetArray "arrSpells", CStr(x) & "," & CStr(y), Empty
Next
Next
World.Note "Arrays Initialized."
End Sub

Sub STest
MCSetArray "arrSpellsPointer", "1", 4
MCSetArray "arrSpells", "1,1", -1
World.Note "Array test values entered."
End Sub

Sub Test
Dim x, y
Dim ArrayValue
For x = World.GetVariable("arrSpellsPointer_LB") to World.GetVariable("arrSpellsPointer_UB")
ArrayValue = MCGetArray("arrSpellsPointer", CStr(x))
World.Note "arrSpellsPointer(" & CStr(x) & "): " & ArrayValue
Next
For x = World.GetVariable("arrSpells_LB1") to World.GetVariable("arrSpells_UB1")
For y = World.GetVariable("arrSpells_LB2") to World.GetVariable("arrSpells_UB2")
ArrayValue = MCGetArray("ArrSpells", CStr(x) & "," & CStr(y))
World.Note "arrSpells(" & CStr(x) & "," & CStr(y) & "): " & ArrayValue
Next
Next
World.Note "Done."
End Sub

If you want to try the test script, you should probably type the following directly into mushclient's command line:

/dtest
/itest
/stest
/test

As you can see with this latter script, I also create MUSHclient variables like "arrSpellsPointer_LB". These are meant to emulate the 'LBound' and 'UBound' functions normally usable on arrays. Notice that they too get deleted when calling MCDelArray (See me warning above)! This is fine with me, since they hold data related to the array. Without an array, I don't need these variables either.

Comments and suggestions are welcome. Feel free to borrow any code that you wish.

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


6,751 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.