Click to See Complete Forum and Search --> : detect part of a line


Shampie
07-10-2003, 11:21 AM
Hello (long time)

I got a question concearning howto detect a part of..

okay I got this:

katana 1d8 (work no problems, since it needs to say 1x 8)

katana 2d8 (works with problems, since it needs to say 2x 8 but will give 1x 8) how can I let my script check out the number before the d? (katana Xd8 where it will read out X)??
any idea? Thank you!

freefall
07-10-2003, 11:28 AM
Your question is quite confusing, let's see.
You have a string, katana 1d8 (which is supposed to be katana 1x8, but for undisclosed reason, it's simply not.)

And you have another string,
katana 2d8 (which acts just like its little bro, katana 1d8)

so you want to find the number before the d (which is supposed to be an x)

var txt = "katana 2d8";
var num = txt.charAt(txt.lastIndexOf("d")-1);

Since you didnt post your script, I dont know how you get the katana strings, but however you do that, make it so txt = the strings.

Oh, you might have multi-digit numbers... Just looks for what's between the space and the d
var num = txt.substring(txt.indexOf(" "), txt.indexOf("d"));

Hope this helps
- Ian

Mr J
07-10-2003, 11:36 AM
Does this help


<script language="Javascript">
<!--

string="1d8"

char_a=string.indexOf("d")
alert(string.charAt(char_a-1))

// -->
</script>

Shampie
07-10-2003, 11:49 AM
Hello thanks for quick responses,

this is the idea:

an item will be generated and when buying added to you weapon list in your inventory:
opt[opt.length] = new Option(""+size+" "+wpnname+" "+diceamount+"d"+diceeye+"", ""+diceeye+"");

for example it will generate a "katana 1d8","8"


a dropdownlist has an item in it:
"katana 1d8","8" (in script)

(the 1d8 stand throwing 1 dice with 8 sides, if it's 2d8 its throwing 2 dices with 8 sides)

now the value will be set to 8 (that's not problem)

I need to have the function to find this out:
(go with xd8)

if "[itemname] 1d8" is selected , x = 1
if "[itemname] 2d8" is selected, x = 2
if "[itemname] 3d8" is selected, x = 3
the 8 can be another number but that will be linked via it's value.. however the x value is not and I need to read it without the dingdongs..

I dont know how to work with string (lets just put it this way, explain me a little and show me what i should do and that should teach me more)

I hope this explains a little more...

Shampie
07-10-2003, 11:59 AM
however the * var txt = "katana 2d8";
var num = txt.charAt(txt.lastIndexOf("d")-1); * script seems to work good! I cannot thank you enough! this will help a lot!

freefall
07-10-2003, 12:04 PM
Okay, how about something like this:

function getRolls()
{
var txt = itemSpecs; // where itemSpecs is the variable that holds the "[item name] 1d8"
var num = txt.charAt(txt.lastIndexOf("d")-1); // txt.charAt() gets the character at the position inside the ().
// In this case, it finds the position where the "d" is located and then looks to the character before it (-1)
// So that tells the charAt() to get the character directly before the "d"
return num;
}


So you'd call this function like this to get x:
x = getRolls();

Hope this is closer to what you need.
- Ian

Shampie
07-10-2003, 12:50 PM
does the same thing go for :
Katana 1d8+y (to read out the y in the "+y" part?
tnx!

freefall
07-10-2003, 12:57 PM
Well if katana xd8+y is the actual string, then the function would be

<script type="text/javascript">
function getRolls()
{
var txt = itemSpecs;
var num = txt.substring(txt.indexOf("+")+1, txt.length);
return num;
}
</script>


That will get the number between the + and the end of the string
- Ian

Shampie
07-10-2003, 03:21 PM
somehow
var num = txt.substring(txt.indexOf("+")+1, txt.length);

okay it will read behind the +, but if there is no + in the line it will give the total name.. I guess I should put in if the line does not have a "+" in it then num = 0..
right or wrong? how is that done?

it does work this way..
var xum = txt.charAt(txt.lastIndexOf("d")+3);

freefall
07-10-2003, 03:48 PM
If you want it to return 0 if there's no +, here's a one-liner, replace the middle line of code in my last submit with this:

var num = ((txt.indexOf("+") != -1)?txt.substring(txt.indexOf("+")+1, txt.length):0);

This is an if statement. It's saying if (txt.indexOf("+") != -1) which means that it found a +, then (denoted by ?) find out what's after it, else (denoted by :) num = 0.

- Ian

Shampie
07-11-2003, 12:50 PM
Thank you for your help! this will decrease the size and amount of scripting which can be set to 'real' random now!