\n in variables

Posted by Chdling on Thu 18 May 2006 05:01 PM — 6 posts, 26,744 views.

#0
i've got a trigger

You follow @target (?P<where>.*).

That runs (sent to script)-
x = GetTriggerWildcard ("follow", "where");
if (x == "north")
{
y = "n";
}
else if (x == "south")
{
y = "s";
}
else if (x == "east")
{
y = "e";
}
else if (x == "west")
{
y = "w";
}
else if (x == "northeast")
{
y = "ne";
}
else if (x == "northwest")
{
y = "sw";
}
else if (x == "southeast")
{
y = "se";
}
else if (x == "southwest")
{
y = "sw";
}
route = world.GetVariable("direction");
world.SetVariable("direction", route + "," + y);

So i end up with ,n,n,e,e,e etc after the trigger fires a few times (first , gets ignored by the MUD anyway)

then i wanted and alias
return -
route = world.GetVariable("direction");
world.Execute(world.ReverseSpeedwalk (route));

Now the problem here is it errors in reverse speedwalk as i have ,'s in the variable and if i try
world.SetVariable("direction", route + "\n" + y);
so everything is on it's own line (the mud will recognise n etc on it's own but won't recognise n<space>s)
it splits the code up instead of sending the next command to a newline in the variable.

So my question is either how do i send to a newline in a variable or how do i get the ReverseSpeedwalk to ignore commas but still send them?
Australia Forum Administrator #1
There must be a simpler way than a huge "if" statement, like a table lookup.

Anyway, to get rid of the commas simply use a find-and-replace to get rid of them before doing the reverse speedwalk.

I'm sure Jscript will have something, and if not use MUSHclient's Replace function:

http://www.gammon.com.au/scripts/doc.php?function=Replace

However you need to fix up the multiple-character directions, like "ne" which will be interpreted as north followed by east.

MUSHclient expects such speedwalks to be rendered as "(ne)" (etc.).

See the documentation for ReverseSpeedwalk for examples of that:

http://www.gammon.com.au/scripts/doc.php?function=ReverseSpeedwalk

If you do that then you don't need the commas anyway, each direction should be recognisable on its own. That is, your built-up string should look like:


nnnee(nw)(ne)wwss(sw)


and so on.

Then you can just reverse it without any further fiddling around.

Amended on Thu 18 May 2006 10:17 PM by Nick Gammon
#2
would

while (world.GetVariable("direction").indexOf(",") > 0 ) {
var k = world.GetVariable("direction").indexOf(",") ;
text = new String(world.GetVariable("direction")) ;
text1 = new String(text.subString(0, k));
text2 = new String(text.subString(k+1, text.length);
text = text1 + text 2;
}

work to root out the , s in that string?
Australia Forum Administrator #3
That looks incredibly complicated. You only need to do a Replace to get rid of the commas. Try this:


s = "n,n,e,e,e,se,sw,u,d,w,n"  // test string
s = world.Replace (s, ",", ")(", true)  // get rid of commas
s = "(" + s + ")"  // put brackets at the end
s = world.ReverseSpeedwalk (s) 
Note (s) // (s)(e)(u)(d)(ne)(nw)(w)(w)(w)(s)(s)
Note (EvaluateSpeedwalk (s))

Output

s
e
u
d
ne
nw
w
w
w
s
s



As I said before you need to take into account directions like "ne" because if you just get rid of commas north followed by east (n,e) will be treated the same as northeast (ne), if you just remove the commas.
#4
Uhm, you're saying it splits up the code... so couldn't you just double escape the newline? Like,
world.SetVariable("direction", route + "\\n" + y);
Australia Forum Administrator #5
Yes, you can double-escape the newline. I still think the big "if" statement looks messy, but if you wanted to use that, the double-escape idea should work.