Extension split

You think you've found a bug? Please report it here.

Moderators: Lapo, Bax

Post Reply
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Extension split

Post by Virusescu »

Sooo Lapo. I think I found a weird bug.
I've been testing for the last half hour and finally managed to separate the error wich seems to be the following.

I'm developing an extension that sends a long message to the client. Because sometime the parameters will vary I made some custom strings that I parse inside the extension.

Somewhere inside the parsing something went wrong.
I'm using the str method for sending the texts.
The code looks something like this

Code: Select all

function handleRequest (cmd, params, user, fromRoom)
{
	switch (cmd)
	{
		case "debug" :
		var response = new Array();
                // here goes the problem
                ppp = params[1].split (" | "); // this is my separator
               for (var x = 0;x<ppp.length;x++) {
                    trace("ppp["+x+"]=" +ppp[x])
               }
This is where the problem occurs. If I split the params[1] (my second parameter) after a my separator the separator will show up inside the array as an element. Maybe the space is confusing? I don't know .. Maybe you can help on this matter.

If I send a message with a string like "Lapi | SFS | Adrian | goflash"
it will output something like this
[thu_challenge.as]: ppp[0] = Lapi
[thu_challenge.as]: ppp[1] = |
[thu_challenge.as]: ppp[2] = SFS
[thu_challenge.as]: ppp[3] = |
[thu_challenge.as]: ppp[4] = Adrian
[thu_challenge.as]: ppp[5] = |
[thu_challenge.as]: ppp[6] = goflash

Weird.. really weird.
The split actually works when called on a string defined in that function but not when it is recieved a a parameter.

Any insights?
function onJoin(usr) {if (usr.getName() == "Lapo") trace ("All Hail Lapo");}
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

It seems that a params[x] recieved as a parameter is not actually an actionscript string ? :shock:

This makes a lot of mess in the rezult array
ppp = params[1].split (" | ");
and this works just fine
ppp = String(params[1]).split (" | ");

I first thought it was the space that bothered the split.. or it worked only with one character... But when I split after the "|" it splited the string by each character ... weird weird weird.
function onJoin(usr) {if (usr.getName() == "Lapo") trace ("All Hail Lapo");}
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

I guess the problem could be in the original type of the string you're trying to split.
Try simple to check its type with:

Code: Select all

trace(typeof(myVar))
If you get "object", then the variables is not a native AS String, if you get "string" then it is and it should work.

In the first case, you can easily cast the variable like this:

Code: Select all

asString = String(myVar)
hope it helps :)
Lapo
--
gotoAndPlay()
...addicted to flash games
Virusescu
Posts: 260
Joined: 07 Sep 2005, 09:36
Location: [RO]Bucharest
Contact:

Post by Virusescu »

In the first case, you can easily cast the variable like this:

Code: Select all

asString = String(myVar)
Yes.. already did that and worked. This is how I solved it.

I'll do some tests and let you know though.
function onJoin(usr) {if (usr.getName() == "Lapo") trace ("All Hail Lapo");}
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

It seems you're getting the data to split from the parameters passed by the server, using the string protocol.

If so you will receive a Java string which can be used as an AS string in 90% of the cases.
In the remaining 10% it can have some different behaviours as it supports more methods and some of them are slightly different from those available in AS.

Here's an overview of the Java String object
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply