Trick for indirect variable reference

Posted by Roga on Sat 05 Oct 2002 12:38 PM — 7 posts, 28,858 views.

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

USA #1
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
Australia Forum Administrator #2
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)


Amended on Sat 05 Oct 2002 10:47 PM by Nick Gammon
USA #3
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
Australia Forum Administrator #4
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.
#5
Nick, could you convert the quicksort into jscript.... I just don't know how to convert the "loops?", for loops? or what...
Australia Forum Administrator #6
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;