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