Shoutbox

Getting the Xth word of a sentence - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Getting the Xth word of a sentence (/showthread.php?tid=94559)

Getting the Xth word of a sentence by Riko15 on 05-10-2010 at 03:06 PM

Does anybody knows how to get the Xth word of a sentence?
In the mIRC scripting language it's $1

So an example: if John says 'Hi, I'm John', I want to have for example the second word 'I'm', what function should I use?


RE: Getting the Xth word of a sentence by matty on 05-10-2010 at 03:51 PM

js code:
var s = 'Hi, I\'m John';
var sArray = s.split(' ');
Debug.Trace(sArray[1]);

/*
    sArray:
        0 = Hi,
        1 = I'm
        2 = John
*/

RE: RE: Getting the Xth word of a sentence by Riko15 on 05-10-2010 at 04:28 PM

quote:
Originally posted by matty
js code:
var s = 'Hi, I\'m John';
var sArray = s.split(' ');
Debug.Trace(sArray[1]);

/*
    sArray:
        0 = Hi,
        1 = I'm
        2 = John
*/


Thnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(Y)
Edit:

Doesn't work? o,o
RE: Getting the Xth word of a sentence by matty on 05-10-2010 at 04:43 PM

Post your code because it does work.


RE: RE: Getting the Xth word of a sentence by Riko15 on 05-10-2010 at 05:31 PM

quote:
Originally posted by matty
Post your code because it does work.

code:
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MsgKind){
    var s = Message;
    var sArray = s.split(' ');
    if(sArray[0] == "!tfd"){
        if(sArray[1] == "users"){
... some gay code of me xd ..
        }
    }
}

RE: Getting the Xth word of a sentence by matty on 05-10-2010 at 05:49 PM

Are these commands you are sending or commands that your contact is sending to you?

Other option is to do something like this:

js code:
function OnEvent_ChatWndReceiveMessage(oChatWnd, sOrigin, sMessage, nMsgKind){
    if (/^([\/|\!][^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        if (RegExp.$1 === '!tfd' && RegExp.$2 === 'users') {
                // put some code here
        }
    }
}

RE: RE: Getting the Xth word of a sentence by Riko15 on 05-10-2010 at 06:14 PM

quote:
Originally posted by matty
Are these commands you are sending or commands that your contact is sending to you?

Other option is to do something like this:

js code:
function OnEvent_ChatWndReceiveMessage(oChatWnd, sOrigin, sMessage, nMsgKind){
    if (/^([\/|\!][^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        if (RegExp.$1 === '!tfd' && RegExp.$2 === 'users') {
                // put some code here
        }
    }
}


Both xD
RE: Getting the Xth word of a sentence by matty on 05-10-2010 at 08:02 PM

You are going to need to explain what you are trying accomplish. Both implementations of what you requested will work. But there may be some limitations of what you can actually do (based on the example you have given).


RE: Getting the Xth word of a sentence by Adeptus on 05-11-2010 at 01:41 AM

Riko15: remember that you probably want to check sArray.length before trying to reference elements that may not be there.  I don't do Plus! scripting specifically, but that's just general coding common sense.