[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  VBscript
. . -> [Subject]  ReDim, UBound & LBound on World [array] Variables.

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: ReDim, UBound & LBound on World [array] Variables.
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Pages: 1 2  

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Thu 26 Jun 2003 01:43 PM (UTC)  quote  ]
Message
I just gave you a function that will search for an item

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Norbert   USA  (61 posts)  [Biography] bio
Date Thu 26 Jun 2003 10:53 AM (UTC)  quote  ]
Message
Well what I was trying to do was to make a alignment bar for my info bar, because my character gets better attacks when he is neutral, and I wanted to use an array instead of a switch since an array would just be one line of code. I know in LPC there is searcha that would search an array for an element, and I've been told that PERL has a similar function, so I'm just going to convert my script to PERL.
Thanks for the help though.

Norbert

-Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens
It's a dumb question... skip it.
[Go to top] top

Posted by Shadowfyr   USA  (1,774 posts)  [Biography] bio
Date Tue 24 Jun 2003 08:35 PM (UTC)  quote  ]
Message
Nope. You can't change the base for the range under VBScript. No such command exists. The only reason, as I said, that VB allows it is to make conversion of older Basic programs to VB easier by allowing you to use the exsiting array structures in such a program.

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Tue 24 Jun 2003 11:42 AM (UTC)  quote  ]
Message
Maybe you can use "Option Base 1", like in VB? Haven't tried it though... I just assume the base is one, and fix any bounds errors that might come up :P

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Shadowfyr   USA  (1,774 posts)  [Biography] bio
Date Tue 24 Jun 2003 12:12 AM (UTC)  quote  ]
Message
Well, in scripting:

"The lower bound of an array is always zero."

However, in Visual Basic you can change the lbound for all arrays used, though the default in that is 1. This is for reverse compatibility with earlier Basic programs that used 1 as the first index, but allows you to instead use 0 as the first one when you want to. As far as I know, VBScript forces you to use the 0 -> (size - 1) version, like most other languages and you can't change it.

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Mon 23 Jun 2003 09:50 PM (UTC)  quote  ]
Message
OK, let me contribute a bit to this thread... I didn't read all of it, because, I'm too lazy, but I think this'll help :P

I made a script to count EQ damage a while ago. Uses two-dimensional VBScript global variables to store the data and then loads/saves them in MC variables when it's done.
Here is a general example of arrays in VB:

Dim varArray(2)
Dim varDamage()
varArray(1) = "Test"
varArray(2) = "Test 2"

Sub AddItem(strItem)
Redim Preserve varDamage(UBound(varDamage) + 1)
varDamage(UBound(varDamage)) = "Store this"
End Sub

That way, you can initialise and resize a variable at runtime. There are many functions to search, store and load arrays to MC variables in my script, ready to be used.
I'm pasting a few because the script is no longer online:

Sub subSaveArray
Dim intCounter
Dim strContents
Dim intArrayCounter

For intCounter = 0 to UBound(strEQTable, 2)
strContents = strEQTable(0, intCounter)
For intArrayCounter = 1 to constTableItems
strContents = strContents & "|" & strEQTable(intArrayCounter, intCounter)
Next
world.SetVariable "EQHit_Item_" & intCounter, strContents
Next
End Sub

Function fnFindItem (strItemName)
Dim intCounter

For intCounter = 0 to UBound(strEQTable, 2)
If UCase(strEQTable(0, intCounter)) = UCase(strItemName) Then
fnFindItem = intCounter
Exit Function
End IF
Next
fnFindItem = -1
End Function

The function to load the array was a bit huge due to script-specific stuff, but I'm sure you can reverse the save one.
VB is a bit hazy when it comes to arrays. I still don't know to this day what the lower bound of a VB array is. I.e. if i declare varArray(7), is the lower bound 0, or 1? and is the high bound 7, or 6? Confusing... Anyway, I hope I helped.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Mon 23 Jun 2003 09:35 PM (UTC)  quote  ]
Message
Another approach is simply to use a database (SQL) and get data directly from that. That would allow very large collections, and be fairly flexible. There are examples of doing that in the plugins section.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Mon 23 Jun 2003 09:33 PM (UTC)  quote  ]
Message
I don't suppose you mean "search a string" - you can use instr for that.

If you are talking about arrays, the easiest is probably to use "for each" and then test each array item for a match.

Certain other languages allow you to set up indexed items, known by various names, for instance "map" or "collection". In these cases, if set up properly, you can do something like this:

i = experience ["nick"]

That is, get the experience from an array indexed by a name, not a number.

Indeed Visual Basic has something similar called a collection, however VBscript does not have collections, I checked.

Shadowfyr's suggestion is correct, a linear search would be slow for a large list, and would be better off turned into a binary search. For small lists the overhead might not be noticeable.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Shadowfyr   USA  (1,774 posts)  [Biography] bio
Date Mon 23 Jun 2003 05:45 PM (UTC)  quote  ]
Message
Umm. I don't think so Norbert. I don't know of any language that does have such a command. Generally arrays require that you provide the search methods, since there are any number of ways you could do it, some more efficient than others with certain types of data. For instance, a sorted array is easiest to find something in using something like a bninary search, which cuts the total size of the section of the array being checked in half until it either can't divide it up to smaller bits any more or finds the value. Example:

array = {a b c d e f g h}
Find = "i"
Start = 1 (a)
End = 8 (h)
Test = int(End - Start)/2 + Start = 3 (c)
c < Find : Start = 3 (c)
Test = int(End - Start)/2 + Start = 5 (e)
e < Find : Start = 5 (e)
Test = int(End - Start)/2 + Start = 6 (f)
f < Find : Start = 6 (f)
Test = int(End - Start)/2 + Start = 7 (g)
g < Find : Start = 7 (g)
int(End - Start)/2 < 1 : Test = 8 (h)
h < Find : Start = 8 (h)
Start = End : Nothing was found.

This means you look at far fewer value, in this case 5 instead of all 8). This is misleading though, since the 'jump' gets bigger the more values you have. For 10,000 you would only check 14 values to figure out that it wasn't in the array. However, this will only work on information you can actually sort, so any other method will tend to require that you search the entire thing from start to end to make sure the value is not there. This is necessary when you 'have to' keep all the stuff in the original order you entered it.

Note.. It is possible to adapt the above method to find the best place for a new value to be inserted into a sorted array. Useful when you can manage to get it working right. ;)

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by Norbert   USA  (61 posts)  [Biography] bio
Date Mon 23 Jun 2003 01:00 PM (UTC)  quote  ]
Message
This sort of fits into this subject, but I was wondering if VB has a function that searches a array for a certain value then return that position?

Norbert

-Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens
It's a dumb question... skip it.
[Go to top] top

Posted by Krenath   USA  (74 posts)  [Biography] bio
Date Tue 01 Jan 2002 05:26 PM (UTC)  quote  ]

Amended on Tue 01 Jan 2002 05:29 PM (UTC) by Krenath

Message
VBscript supports the functions JOIN() and SPLIT().

JOIN takes an array and a delimiter (such as ",") and returns a delimited string which can be crammed into a MUSHclient variable with little problem

SPLIT takes a string and a delimiter and chops the string apart wherever it finds the delimiter, returning an array.

You could easily do the following:
Quote:

Dim aArray
aArray=Array("Foo","Bar","Baz","Gleep")

World.SetVariable "ArrayTest",Join(aArray,":")

'ArrayTest now contains "Foo:Bar:Baz:Gleep"
'I don't have to use a colon. I normally use a comma,
'but all the commas makes my example harder to read.
'You could choose a space, a tilde, the string " and ",
'or whatever you want to use to separate your array
'elements. You don't have to rigidly stick to the
'example.


Later in your script, you can get your array back like so:

Quote:

Dim aArray,i
aArray=Split(World.GetVariable("ArrayTest"),":")

For i = lbound(aArray) to Ubound(aArray)
World.Note "element " & i & " contains " & aArray(i)
next


Using JOIN and SPLIT to turn your arrays into strings and back means you don't have to *simulate* using arrays. You really can use actual arrays. You simply use MUSHclient variables to store the contents, and load them into arrays when you want to work with them.

If you work with multidimensional arrays, you'll have to use JOIN and SPLIT inside loops or something, and you'll use a different delimiter for each dimension. A two-dimensional array rendered as a string might look like this:

foo1,bar1,baz1,gleep1:foo2,bar2,baz2,gleep2:foo3,bar3,baz3,gleep3

Split the string on : to get substrings. Split the substrings on , to get individual elements. Store the results in a two-dimensional array.

- Krenath from
bDv TrekMUSH
ATS TrekMUSH
TNG TrekMUSE
TOS TrekMUSE
[Go to top] top

Posted by Magnum   Canada  (580 posts)  [Biography] bio
Date Fri 28 Dec 2001 11:40 AM (UTC)  quote  ]
Message
Shadowfyr (Who's AoD alias I still can't figure out):

Although I did programming in BASIC and PASCAL over 10 years ago, I haven't done any since... until now. A lot of programming fundamentals are hazy to me.

I seem to recall, in PASCAL, that a main program could inherit variables from any libraries that it called.

I have to assume that Mushclient runs the equivelant of it's own mainline internally, and then calls the script file. The internal program must never terminate, otherwise all variables inherited by the script 'library' would be lost.

My previous assumption was that there was no internal main program, so whenever script subroutines were called, they would be loaded, run, and then unloaded (deleted).

...but however the internal programming works, what's important is that I now understand it. With my large script, which keeps growing, I should be able to eliminate some of the mushclient variables and replace them with VB Global variables. (The ones where I don't care if they get tossed when Mushclient is closed, or the script reinitialized).

Anyway, enough of that, and on with the project!

I've developed some new script routines for emulating an array using mushclient variables. I will post the newest script in a new thread, so that anyone borrowing from my work will not have to read through all learning steps I had to take along the way.

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

Constantly proving I don't know what I am doing...
Magnum.
[Go to top] top

Posted by Shadowfyr   USA  (1,774 posts)  [Biography] bio
Date Fri 28 Dec 2001 09:12 AM (UTC)  quote  ]
Message
You can probably look at it this way, scripts act like programs without the main() declared. Anything not specifically defined as a sub is simply run. QBASIC did the same thing, allowing you to create optional subroutines that could be called by name, instead of using gosub 1000, or such. Everything in the main part of the script runs first, including global variable declarations, but like with QBASIC, loading or reloading the script causes it to be treated as a new program, and you lose all the previous settings. With QBASIC there was a variable saving command, that could be used to get around this, I think (may be confusing it with the old Apple BASIC). But since vbscript wasn't really designed to run long term, just when needed by a web page, it wasn't provided with any such command that I know of.

However... Assuming you have certain programs installed, you may be able to use SQL commands to call/create a database, then plug the values into that database file and retrieve them the same way. Only, I am not sure what the minimum needed for you to do this is. I have seen some hints that indicate MS Excel and even Word can be remote called in some cases, but not exactly how it works or what can actually be done with them. On the other hand, installing MYSQL or something like it would provide the 'server' needed to send/recieve database requests. The data could thus be stored, saved and later retrieved from the database, without worrying about the script getting reloaded, since it would exist independant of the script itself. The only draw back is you have to make sure that the ORB driver (server for SQL) is running before calling the script, otherwise it obviously isn't going to work. ;)

Sadly due to the security issues involved with the way vbscript was designed, there is no other way to do this, unless there is some way to generate a cookie file, like used in browsers. However, that may be a function of the client architecture the script runs in, not the language, so mushclient may not make such a method possible. Best bet though would be the database. Ironic that MS thought to supply a set of ODBC drivers with all versions of windows, but doesn't provide even a very simple server app to access them. :p You can use the control panel for ODBC to create one of any type for which the file structures are installed, just not access any of them. :p

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 27 Dec 2001 10:21 PM (UTC)  quote  ]
Message
If you take a look at the example vbscript file that comes with MUSHclient, there is a line right at the end ...



world.note "Scripting enabled - script file processed"


Because this line is outside a sub, it is executed when scripting is enabled (for that world), ie. when the world file is loaded. Thus you could use that to do some initialisation.

Yes, global variables will persist from call to call (ie. dims that are outside a sub), however you still have the problem that I mentioned that if you rerun scripting (eg. if you change the script file) then it is re-executed, and all global variables are lost (however the initialisation line is re-executed).

Thus, to make things persist, you would need to save them (eg. to MUSHclient variables) when you change them, and reload them when the script file is re-executed.

Given that, I tend to use MUSHclient variables all the time, as they persist even when you change the script file, and will persist forever if you remember to save the world file (the script can do that if you want).

However for complex things like arrays, you have to fiddle a bit to put arrays into variables. One technique, other than the one I described earlier, is to use a special character (eg. tab) to delimit occurrences of an array (when putting into a variable) and then using an "explode" function (I don't remember its exact name) to get them back from the variable into an array.

I think there are examples on the forum.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Magnum   Canada  (580 posts)  [Biography] bio
Date Thu 27 Dec 2001 07:37 AM (UTC)  quote  ]
Message
Ahh... Ok, I see the error of my ways. Looks like I can delete the mass of "Dim" statements at the beginning of my script file.

There is one thing though, and this is an assumption that I have been working on since I started scripting:

I've always thought of the mushclient script file as a library file. That is, subroutines are called, executed, and then forgotten (deleted from memory). There is no 'base' script program that is ALWAYS running, and thus, no 'global' variables that are ALWAYS stored in memory.

So... using the correct language, is it possible to even use global variables in a mushclient script? What would happen if I took my "World_Initialize" script, and removed the Sub and End Sub header/footer? Would it get executed once when I load mushclient (or re-process script)?

Assuming I want to maintain the spells in my spell queue across MushClient sessions:

Given this new information, it seems I would have to manage LBound and UBound myself... and the equivelent to 'ReDim' would be a series of "World.DeleteVariable" executions. It all seems feasible.

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

Constantly proving I don't know what I am doing...
Magnum.
[Go to top] 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.


19,010 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]