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
➜ Jscript
➜ translate from VBscript
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Avariel
Portugal (55 posts) Bio
|
Date
| Mon 30 Sep 2002 01:37 PM (UTC) Amended on Thu 03 Oct 2002 11:54 PM (UTC) by Nick Gammon
|
Message
| i only know basic in jscript code but i know even less in vbscript code... Shadowfyr sugested me to use this function in vbscript but im not able to translate it to jscript.
can someone help me?
ohh btw... thx again Shadowfyr.
the function is show below.
thx.
------------------------------------------------------------
A binary search in case you don't know basically does this:
function find (name)
dim names, walks 'Storing your location names and speed walks.
names = split(world.getvariable("???"),",") 'What ever ones you use.
walks = split(world.getvariable("???"),",")
w
fd = false
st = 0
en = ubound(record)
do until fd or st = en
cr = st + int((en-st)/2)
if names(cr) > name then
en = cr
else if names(cr) < name then
st = cr
else
fd = True
end if
if le = en and lst = st then 'Otherwise it goes forever if no match exists.
exit do
else
lst = st
le = en
end if
loop
if fd then
find = walks(cr)
else
find = "Unknown!" 'Check for this in the calling script (since sending it is pointless.) ;)
end if
end function
Modified by Nick to add [code] and [/code]
------------------------------------------------------------ |
The Avariel Wind Lord | Top |
|
Posted by
| Shadowfyr
USA (1,791 posts) Bio
|
Date
| Reply #1 on Mon 30 Sep 2002 07:05 PM (UTC) Amended on Wed 02 Oct 2002 07:02 PM (UTC) by Shadowfyr
|
Message
| I am going to try to take a crack at this anyway. P.S. It helps a 'lot' if you click the little Forum codes box below the input box in this forum and
enclose all 'code' between [code] and [\code]. Posting stuff that should be indented without those makes it very unreadable. ;)
function find (name) {
var temp = new StringTokenizer(world.getvariable("names"),","); //Replace 'names' with the variable you are using for them.
var names = new arraylist()
while (temp.hasMoreTokens()) {
names.add(temp.nextToken());
}
temp = new StringTokenizer(world.getvariable("walks"),","); //Replace 'walks' with the variable you are using to store them.
var walks = new arraylist()
while (temp.hasMoreTokens()) {
walks.add(temp.nextToken());
}
var fd = false;
var st = 0;
var en = names.size();
var cr;
var le = 0;
var lst = 0;
do {
cr = st + int((en-st)/2);
if (names(cr) > name)
en = cr;
else {
if (names(cr) < name)
st = cr;
else
fd = True;
}
if (le = en and lst = st) //Otherwise it goes forever if no match exists.
break; //'exit do' would be better and more specific, but apparently they wan't it to be confusing. lol
// Not to mention there being some question in my mind as to how you force an exit of a sub or
// other nested object if you can't specifically tell it what you are 'break'ing out of. :p
else {
lst = st;
le = en;
}
while not(fd) and st <> en}
if (fd)
find = walks(cr);
else
find = "Unknown!"; //'Check for this in the calling script (since sending it is pointless.) ;)
}
Now.. As I said I don't know javascript hardly at all and don't claim that this can or will work right. Prehaps if Nick or
someone gave it a look over first, but I have no idea if it work work as written. Specifically... The use of 'Arraylist' and
StringTokenizer. These 'should' collectively work like 'split' in VB, but I really can't say for certain. Most of this is
pulled a bit at a time from a Java manual and may work differently or not at all under Javascript.
Anyway, good luck and hopefully someone will look this over for you, instead of glossing over it like they seem to have done
with the VB version (which had several errors).
| Top |
|
Posted by
| Avariel
Portugal (55 posts) Bio
|
Date
| Reply #2 on Mon 30 Sep 2002 11:55 PM (UTC) |
Message
| can someone help plsssssssssssss
thx. |
The Avariel Wind Lord | Top |
|
Posted by
| Shadowfyr
USA (1,791 posts) Bio
|
Date
| Reply #3 on Wed 02 Oct 2002 07:15 PM (UTC) |
Message
| Heh **Nick**. We really do need help figuring out how/if this will work. I don't even know how to do if-then right in jscript. This generated an error no matter how I tried it:
if (le = en and lst = st)
Since I didn't get past this... I couldn't test any of the rest of it to see if it worked. I tried my best guess about how it all should work in jscript, but I just don't know the language. :p | Top |
|
Posted by
| Avariel
Portugal (55 posts) Bio
|
Date
| Reply #4 on Thu 03 Oct 2002 02:48 PM (UTC) |
Message
| i think i can help on that
== equal
!= not equal
|| logical or
&& logival and
so i think that expression should be
if ( le == en && lst == st ) {
}
hope this can help you helping me :)
|
The Avariel Wind Lord | Top |
|
Posted by
| Shadowfyr
USA (1,791 posts) Bio
|
Date
| Reply #5 on Thu 03 Oct 2002 07:09 PM (UTC) |
Message
| I tried that and still got an error... I just don't know and no one else seems to be responding to this post. :p | Top |
|
Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
Date
| Reply #6 on Fri 04 Oct 2002 12:37 AM (UTC) Amended on Fri 04 Oct 2002 12:39 AM (UTC) by Nick Gammon
|
Message
| OK, sorry for the delay, I was away for a few days. :)
This works:
function find (name) {
var names_string, names_array
var walks_string, walks_array
var found, start, end, current
var last_start, last_end
// get names from variable, as a comma-delimited string
names_string = world.getvariable("names");
// convert into an array
names_array = names_string.split (",");
// get walks from variable, as a comma-delimited string
walks_string = world.getvariable("walks");
// convert into an array
walks_array = walks_string.split (",");
found = false; // not found yet
start = 0; // start at left
end = names_array.length; // end at right
last_start = 0; // for checking end of loop
last_end = 0; // ditto
// loop until found
while (!found || start != end)
{
// find half-way point
current = start + ( (end - start) >> 1);
// if name is > this position, it must be to the left
if (names_array [current] > name)
end = current;
// if name is < this position, it must be to the right
else if (names_array [current] < name)
start = current;
else
{
found = true;
break;
}
// this check makes sure we don't loop indefinitely
if (last_end == end && last_start == start)
break;
else
{
last_end = end;
last_start = start;
}
} // end of while loop
if (found)
return walks_array [current];
return "Unknown!";
} // end of function find
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
Date
| Reply #7 on Fri 04 Oct 2002 12:42 AM (UTC) |
Message
| It goes without saying, I hope, that for a binary search the array must be in alphabetical order. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Avariel
Portugal (55 posts) Bio
|
Date
| Reply #8 on Fri 04 Oct 2002 01:54 PM (UTC) |
Message
| thanks to Shadowfyr for trying and thanks to Nick for that function... |
The Avariel Wind Lord | 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.
31,697 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top