Author Topic: Help with inStr  (Read 1177 times)

Okay so I have an issue that I saw the answer for before but could not find it now, I have searched and looked through the pages I thought it would be.

Sorry for yet another topic guys, but here's my question.

How would I get the location of a letter in a chat message

Like I want something like this:

Code: [Select]
package blabla
{
function clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
{
parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%fmsg);
if(%name $= $Pref::Player::Netname)
{
                 
                   if(inStr(%msg,"help"))
                   {
                       blabla
                   }

}
}
};
   

Anyways seeing as that explained next to nothing, I want something that when I type

JazZ: Hi there buddy !
It will send a message saying "JazZ: Hello"

But if I type

JazZ: Buddy, hi!
It will not send because the word "Buddy" is not the 3rd word or starts with the 10th letter

Is this possible with a client script?


http://forum.blockland.us/index.php?topic=205846.0
This should help
That's exactly what I saw before, and was searching for.  Wow.
Thanks treynolds

You're welcome,
don't lock this topic

You're welcome,
don't lock this topic
Actually I'm not because I have another question going with this one, in that topic they only speak about finding one exact letter, what if the letter is located twice?

Like:

strpos("this x is an example  x","x")
What would that return?

Just realized I've been asking the wrong questions

Answer the last one please, but more important can I pull the letter/number/phrase from %msg if I know the position?

Like
JazZ: Hi 5

How would I make a function return the "5"

Just realized I've been asking the wrong questions

Answer the last one please, but more important can I pull the letter/number/phrase from %msg if I know the position?

Like
JazZ: Hi 5

How would I make a function return the "5"
Use getWord(%source,%index) if the thing you want is always surrounded by spaces, or use getSubStr(%source,%startIndex,%length) if it isn't

Use getWord(%source,%index) if the thing you want is always surrounded by spaces, or use getSubStr(%source,%startIndex,%length) if it isn't

So would %msg be a correct source?

Like

%index = "Hello"
getWord(%msg, %index)

Actually I'm not because I have another question going with this one, in that topic they only speak about finding one exact letter, what if the letter is located twice?

Like:

strpos("this x is an example  x","x")
What would that return?
strPos has another optional argument, offset. It'll start the search that many characters in.
The first x is at 5, the second it as 14, the third is at 22
So, if 0 < offset < 5, the function would return 5.
if 5 < offset <= 14, the function will return 14
if 14 < offset <= 22, the function will return 22.
if x > 22, the function will return -1 (not found)

strPos has another optional argument, offset. It'll start the search that many characters in.
The first x is at 5, the second it as 14, the third is at 22
So, if 0 < offset < 5, the function would return 5.
if 5 < offset <= 14, the function will return 14
if 14 < offset <= 22, the function will return 22.
if x > 22, the function will return -1 (not found)

So the offset would be written as
strPos("asdf", "d", 2) ?

EDIT;
Why does
Code: [Select]
package WordTest
{
function clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
{
parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%fmsg);
if(%msg == Hi there)
{
%wordvar = getWord(%msg, 4);
commandToServer('messagesent',"[RedBot]: This " @ %wordvar);
}
}
};
activatePackage(WordTest);
Not work?
« Last Edit: August 27, 2012, 08:13:39 PM by ¥ola »

1. So the offset would be written as
strPos("asdf", "d", 2) ?

EDIT;
2. Why does
Code: [Select]
package WordTest
{
function clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
{
parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%fmsg);
if(%msg == Hi there)
{
%wordvar = getWord(%msg, 4);
commandToServer('messagesent',"[RedBot]: This " @ %wordvar);
}
}
};
activatePackage(WordTest);
Not work?
-derp-

2. because you didn't use $= and didn't put "'s around Hi there.
« Last Edit: August 27, 2012, 08:48:40 PM by Ipquarx »

1. No, strPos only has 2 arguments, %haystack and %needle (good way of describing it)
Wrong
strPos has another optional argument, offset.


For a second I thought inStr was a function I've never heard of.

So I'm trying this exact code:
Code: [Select]
package WordTest
{
function clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
{
parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%fmsg);
if(%name $= $Pref::Player::Netname)
{
%wordvar = getWord(%msg, 4);
switch$(%msg)
{
case "Hi there":
commandToServer('messagesent',"[RedBot]: This " @ %wordvar);
}
}
}
};
activatePackage(WordTest);
It runs with no syntax errors but it will not sent the message if I type Hi there, any ideas why?


Here's the packaged addon if you need it

http://www.mediafire.com/?1aboghc5plhv1c9