Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   String Parsing (https://forums.graalonline.com/forums/showthread.php?t=79091)

Toxen 03-20-2008 12:11 AM

String Parsing
 
Say I had an editable text box and when the person entered a number in it I needed the number for something like:

if(player.gralats = "Params[1] * 15") {

Would their be anyother way to do it then string parsing , or would anyone like to explain to me in detail about string parsing?

-Thanks in advance
Toxen

Tigairius 03-20-2008 12:17 AM

Not sure what you're asking.
If you only want it to display the first 15 characters then you can do: params[1].substring(0, 15)

if you want to cut off the first 15 characters you can do: params[1].substring(15)

If you want to cut off all the characters before "15" you can do:
params[1].substring(params[1].pos(15))

Also you should use == for comparison. Using one = is for declaration.
if (player.rupees == params[1].substring(15)) { for example.

If you just need to parse a string you could do:
params = obj.text.tokenize();

then use params[0] for the first space. You can define which character to parse like this:
params = obj.text.tokenize("a");
if obj.text = "123abc123abc";
params[0] would = 123
params[1] would = bc123
and so on.

DrakilorP2P 03-20-2008 12:39 AM

PHP Code:

temp.strVar "42";
temp.intVar int(temp.strVar); 


Toxen 03-20-2008 12:40 AM

Well Tig, I'm wondering how to take in the text and then use it right after in the same event, like I have an edit able text area and they put a number in there to see how many packages of that item, they'd like so it's not like a set text yet, so how would I do it?

Tigairius 03-20-2008 01:59 AM

Quote:

Originally Posted by Toxen (Post 1380922)
Well Tig, I'm wondering how to take in the text and then use it right after in the same event, like I have an edit able text area and they put a number in there to see how many packages of that item, they'd like so it's not like a set text yet, so how would I do it?

obj.text

function Obj.onAction() {
player.chat = "I typed in" SPC obj.text;
}

Programmer 03-20-2008 02:14 AM

I believe he's trying to do something like so:

PHP Code:

temp.var1 4;
temp.str "temp.var1 + 1";
temp.value int(temp.str);

echo(
temp.value);

// he wants it to return 5, but it returns -1, naturally. 

Would be interesting to figure this out. Graal needs an eval() function.

cbk1994 03-20-2008 02:25 AM

Chompy released a parser like this, and I released one a while back.

zokemon 03-20-2008 05:32 AM

Kinda difficult when dealing when []'s and such but still possible.

Clockwork 03-20-2008 06:06 AM

substring(0, 15)

hehe...I learned this in VB today. ^^

~Aeko...slowly understanding scripts letter by letter~

Tolnaftate2004 03-20-2008 09:57 AM

Quote:

Originally Posted by Programmer (Post 1380944)
I believe he's trying to do something like so:

PHP Code:

temp.var1 4;
temp.str "temp.var1 + 1";
temp.value int(temp.str);

echo(
temp.value);

// he wants it to return 5, but it returns -1, naturally. 

Would be interesting to figure this out. Graal needs an eval() function.

I don't see why we need to parse a string if we're doing simple math...
text*15 or float(text)*15 should suffice, but if there's something I'm missing here, might I recommend this script (though I hate to plug my own script >_<). It has the necessary code to do this, probably
PHP Code:

temp.result where_param_format("","",whatever_text,15,"times"); 

Not totally sure on this though.

Though, I'm willing to help make a limited eval() if there is enough demand for it.

Programmer 03-20-2008 12:13 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1381011)
I don't see why we need to parse a string if we're doing simple math...
text*15 or float(text)*15 should suffice, but if there's something I'm missing here, might I recommend this script (though I hate to plug my own script >_<). It has the necessary code to do this, probably
PHP Code:

temp.result where_param_format("","",whatever_text,15,"times"); 

Not totally sure on this though.

Though, I'm willing to help make a limited eval() if there is enough demand for it.

From what he told me, he's trying to make a text box and allow people to input simple mathematics and variables, and then translate them to return a readable value. The script I posted was merely an example of what he essentially wants done.

Chompy 03-20-2008 02:29 PM

Quote:

Originally Posted by Programmer (Post 1381022)
From what he told me, he's trying to make a text box and allow people to input simple mathematics and variables, and then translate them to return a readable value. The script I posted was merely an example of what he essentially wants done.

But if that was possible, making a calculator in a programming language would be easy as pie..

I've started a parser that is suppose to parse math stuff like 1+1, a+1=2 etc. but I got bored with it and couldn't decide if I wanted to include variables or not and a lot of other stuff..

Tigairius 03-20-2008 05:58 PM

Toxen, you could do something like this (although there are a few ways):
NPC Code:

if (obj.text.pos("*") > 0) {
temp.token = obj.text.tokenize("*");
player.chat = temp.token[0] @ "x" @ temp.token[1] @ "=" @ temp.token[0] * temp.token[1];
}


and certainly better ways if you're planning on making a calculator or so.

zokemon 03-20-2008 07:19 PM

It's funny because my programming assignment for this week is to make a calculator that converts an in-fixed expression to a post-fixed expression and then calculates the post-fixed expression and returns the result.

Probably that method would be the best for an eval() also. I guess I could make one when I do my assignment on Friday (due date, duh). Oh it also involves stacks.

Inverness 03-20-2008 10:03 PM

Email Google, ask for theirs, then convert it to GS2 :D

cbk1994 03-20-2008 10:24 PM

Quote:

Originally Posted by Inverness (Post 1381123)
Email Google, ask for theirs, then convert it to GS2 :D

They wouldn't give you it ;)

Programmer 03-21-2008 12:42 AM

Quote:

Originally Posted by Inverness (Post 1381123)
Email Google, ask for theirs, then convert it to GS2 :D

Google's is simple.

PHP Code:

$q "1+8";
$evalstr "echo " $q ";";

eval(
$evalstr); 

You can't convert that to gScript unless we have an eval() function *nods to Stefan*

Inverness 03-21-2008 01:38 AM

Quote:

Originally Posted by Programmer (Post 1381208)
Google's is simple.

What an ignorant thing to say.
http://www.google.com/help/calculator.html

Anyhow, it sounds to me like Toxen is just asking how to use a string number in calculations, if that is the case then Graal converts it automatically.

newnumber = TextEditObject.text * 5;

Tolnaftate2004 03-21-2008 01:45 AM

Quote:

Originally Posted by Inverness (Post 1381227)
What an ignorant thing to say.

PHP has perl regular expressions. This is still easily accomplished.

Toxen 03-21-2008 04:32 PM

I actually used
[PHP]

temp.arr = params[0].tokenize();
for (temp.i=0; temp.i<temp.arr.size(); temp.i++) {
temp.arr[temp.i] = int(temp.arr[temp.i]);
}

and it worked so.

Tolnaftate2004 03-22-2008 05:54 AM

1 Attachment(s)
Quote:

Originally Posted by Tolnaftate2004 (Post 1381011)
Though, I'm willing to help make a limited eval() if there is enough demand for it.

Quote:

Originally Posted by Programmer (Post 1381208)
You can't convert that to gScript unless we have an eval() function

Attached is a script for eval() for doing math. It can handle almost any gscript operator right now ('new' and ?: I have omitted, and 'in' because this does not yet support arrays for all I know). It recognizes this. variables in a very elementary way. Strings, GUI objects/TStaticVar/etc, and functions are not supported at this time, as well as op= operators, ++ pre and post increment operators, -- pre and post decrement operators, and the like.

I would perhaps make this a class and join it to a static variable.
PHP Code:

echo(this.eval("((2&&3)+(this.f=97.3))")); // 98.3
echo(this.f);                              // 97.3 


zokemon 03-22-2008 06:02 AM

Dangerous to use eval for assignment or modification of variables though.

Tolnaftate2004 03-22-2008 06:10 AM

Quote:

Originally Posted by zokemon (Post 1381525)
Dangerous to use eval for assignment or modification of variables though.

Doesn't take much to disable it.

cbk1994 03-22-2008 06:33 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1381522)
Attached is a script for eval() for doing math. It can handle almost any gscript operator right now ('new' and ?: I have omitted, and 'in' because this does not yet support arrays for all I know). It recognizes this. variables in a very elementary way. Strings, GUI objects/TStaticVar/etc, and functions are not supported at this time.

I would perhaps make this a class and join it to a static variable.
PHP Code:

echo(this.eval("((2&&3)+(this.f=97.3))")); // 98.3
echo(this.f);                              // 97.3 


Awesome.

Tolnaftate2004 03-22-2008 09:33 PM

I've updated the script above:
  • added protected variables, you can keep eval from modifying some variables by adding to this.
  • added the option to disable the use of assignment operator (won't break evaluation, though).
  • for those who would like to use it, the operator is fixed
  • added limited support for @ (strictly for concatenation)
  • restored precedence (for the most part), fixing the != operator

I wonder if I should finish it? Would eval be beneficial to anyone? Does anyone need it to do something particular?

Toxen 03-24-2008 01:56 AM

Thank you all so much, and especially to you pfa, that is a pretty nice script.

Rapidwolve24 03-24-2008 03:03 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1381522)
Attached is a script for eval() for doing math. It can handle almost any gscript operator right now ('new' and ?: I have omitted, and 'in' because this does not yet support arrays for all I know). It recognizes this. variables in a very elementary way. Strings, GUI objects/TStaticVar/etc, and functions are not supported at this time, as well as op= operators, ++ pre and post increment operators, -- pre and post decrement operators, and the like.

I would perhaps make this a class and join it to a static variable.
PHP Code:

echo(this.eval("((2&&3)+(this.f=97.3))")); // 98.3
echo(this.f);                              // 97.3 


Good work.
This guy I like :D

cbk1994 03-24-2008 03:07 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1381621)
I've updated the script above:
  • added protected variables, you can keep eval from modifying some variables by adding to this.
  • added the option to disable the use of assignment operator (won't break evaluation, though).
  • for those who would like to use it, the operator is fixed

I wonder if I should finish it? Would eval be beneficial to anyone? Does anyone need it to do something particular?

Yes, please finish it! :)

Tolnaftate2004 03-24-2008 04:29 AM

Quote:

Originally Posted by cbkbud (Post 1381894)
Yes, please finish it! :)

Allright. I will make a new thread for this.

zokemon 03-25-2008 10:16 PM

I made mine finally!


All times are GMT +2. The time now is 05:57 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.