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
➜ Tips and tricks
➜ Trick for indirect variable reference
|
Trick for indirect variable reference
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Roga
(7 posts) Bio
|
| Date
| Sat 05 Oct 2002 12:38 PM (UTC) |
| Message
| Thought this might come in handy. It's a method to indirectly reference a variable.
world.setvariable CStr( world.getvariable ("capture_output") ), world.getvariable ("capture_text")
What this does is take the contents of the variable "capture_text" and stores it to the variable name contained in "capture_output".
For instance, if "capture_text" = "This sentence is a test." and "capture_output" = "var_test". Then the above will set "var_test" = "This sentence is a test.". For my setup I already had the "var_test" variable defined.
| | Top |
|
| Posted by
| Shadowfyr
USA (1,792 posts) Bio
|
| Date
| Reply #1 on Sat 05 Oct 2002 06:36 PM (UTC) |
| Message
| Hmm. My first I thought was "Why?", then I realized that it might actually provide a really good idea. ;) lol
Lets perform some magic..
alias> Addwalk * *
script> Addwalk
sub Addwalk(name, output, wilds)
dim a
a = world.getvariablelist
dim wname
wname = "Wlk_" & wilds(1)
for count = 0 to ubound(a)
if wname = a(count) then
world.note "Already exists!"
else
world.setvariable wname, wilds(2)
end if
next
end sub
Then...
alias> Walk *
script> Walk
sub Walk(name, output, wilds)
dim wname
wname = "Wlk_" & wilds(1) dim a
a = world.getvariablelist 'Returns a sorted list, so lets do a binary search. ;)
fd = false
st = 0
en = ubound(a)
do until fd or st = en
cr = st + int((en-st)/2)
if a(cr) > wname then
en = cr
else if a(cr) < wname then
st = cr
else
fd = True
end if
if le = en and lst = st then
exit do
else
lst = st
le = en
end if
loop
if fd then
dim walk
walk = getvariable(a(cr))
world.send world.evaluatespeedwalk(walk)
else
world.note "Unknown!"
end if
end sub
You paying attention Avariel? Modifying yours to do this would eliminate the need to sort the list of names to use the search. ;) Wish I had thought of this method when trying to help you out before. lol | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #2 on Sat 05 Oct 2002 10:43 PM (UTC) Amended on Sat 05 Oct 2002 10:47 PM (UTC) by Nick Gammon
|
| Message
|
Quote:
a = world.getvariablelist 'Returns a sorted list, so lets do a binary search. ;
No it doesn't.
world.getvariablelist is not documented to return a sorted list, nor does it.
You need to sort the array first. I found this quicksort algorithm for VBA which should work for VBscript with minor modifications ...
' Sort the items in array values() with bounds min and max.
Sub Quicksort(values() As String, _
ByVal min As Long, _
ByVal max As Long)
Dim med_value As String
Dim hi As Long
Dim lo As Long
Dim i As Long
' If the list has only 1 item, it's sorted.
If min >= max Then Exit Sub
' Pick a dividing item randomly.
i = min + Int(Rnd(max - min + 1))
med_value = values(i)
' Swap the dividing item to the front of the list.
values(i) = values(min)
' Separate the list into sublists.
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While values(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
' The list is separated.
values(lo) = med_value
Exit Do
End If
' Swap the lo and hi values.
values(lo) = values(hi)
' Look up from lo for a value >= med_value.
lo = lo + 1
Do While values(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
' The list is separated.
lo = hi
values(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
values(hi) = values(lo)
Loop ' Loop until the list is separated.
' Recursively sort the sublists.
Quicksort values, min, lo - 1
Quicksort values, lo + 1, max
End Sub
You would use it like this:
a = world.getvariablelist
Quicksort a, 1, ubound(a)
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Shadowfyr
USA (1,792 posts) Bio
|
| Date
| Reply #3 on Sat 05 Oct 2002 11:39 PM (UTC) |
| Message
| Oops... I didn't check it against my normal worlds, but instead against a test one I use. I had assumed that I added the names in it in no specific order, I guess they actually where. :p Also since the variables are sorted in the list in the variable editor, I assumed that fact, combined with my test gaving me a sorted list, that it did.
Sigh.. Some days trying to do scripting is a bit like being a caveman trying to figure out why a rock sinks while a chunk of wood floats, then one day throwing a stick in and having it sink instead. lol | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #4 on Sun 06 Oct 2002 01:06 AM (UTC) |
| Message
| It is actually a hash table lookup, so they won't even appear in the order they were added, sorry to correct that impression.
Conceivably the hash system might give a pseudo-order to some variables, but I certainly wouldn't rely on it. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Smeagol
(10 posts) Bio
|
| Date
| Reply #5 on Sun 13 Oct 2002 03:29 AM (UTC) |
| Message
| | Nick, could you convert the quicksort into jscript.... I just don't know how to convert the "loops?", for loops? or what... | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #6 on Sun 13 Oct 2002 05:04 AM (UTC) Amended on Sun 13 Oct 2002 05:05 AM (UTC) by Nick Gammon
|
| Message
| I'll take a look tomorrow, but here is a clue about the loops, which I haven't tested:
VBscript
Do While values(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
Jscript
while (values [lo] < med_value)
if (lo++ >= hi)
break;
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | 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.
27,430 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top