Shoutbox

Replacing Words from an Array - 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: Replacing Words from an Array (/showthread.php?tid=88025)

Replacing Words from an Array by KooKas on 12-31-2008 at 07:18 AM

Hello, this is my first post here, I'm just going to ask how I sort this out.
I'm trying to make a translator script from normal chat to chavspeak but currently it does not work. It should turn hi into sfe, hey into wagwan, hello into yo and helo into oi. It does not do that, except it does turn helo into oi.
Can someone help me with it please?

code:
function OnEvent_Initialize(MessengerStart)
{
}
function OnEvent_ChatWndSendMessage(ChatWnd, Message)
{   
    var Greet1 = new Array();
    Greet1[0] = "hi";
    Greet1[1] = "hey";
    Greet1[2] = "hello";
    Greet1[3] = "helo";
   
    var Greet2 = new Array();
    Greet2[0] = "sfe";
    Greet2[1] = "wagwan";
    Greet2[2] = "yo"
    Greet2[3] = "oi"   
   
    var x=0;

    for (x=0; x<4; x++)
    {
        ChatWnd.EditText = Message.replace(Greet1[x], Greet2[x]);
    }
}
function OnEvent_Uninitialize(MessengerExit)
{
}


RE: Replacing Words from an Array by Matti on 12-31-2008 at 09:29 AM

ChatWnd.EditText is the text currently being typed by the user. Because you're using the OnEvent_ChatWndSendMessage, this will be empty as the message is already about to be sent and therefore the input area has been cleared. To change the message to be sent, you need to return the modified message at the end of your function.

Also, your replacing won't work either. First, you replace "hi" with "sfe" and store it in EditText. Then, you replace "hey" with "wagwan" in the original message and overwrite EditText, resulting in the first replacing being lost! In the end, you'll end up with only "helo" being replaced by "oi". A very simple solution is by saving the replacement result in the same variable as the one used for the replacing, which is "Message".

So, a fix for your code could be the following:

code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message)
{   
    var Greet1 = new Array();
    Greet1[0] = "hi";
    Greet1[1] = "hey";
    Greet1[2] = "hello";
    Greet1[3] = "helo";
   
    var Greet2 = new Array();
    Greet2[0] = "sfe";
    Greet2[1] = "wagwan";
    Greet2[2] = "yo"
    Greet2[3] = "oi"   

    // No need to declare variable x twice

    for (var x=0; x<4; x++)
    {
        Message = Message.replace(Greet1[x], Greet2[x]);
    }

    return Message;
}
Have a look at the changes and the explanation and make sure you understand what went wrong, so you can learn from it and won't make the same mistake again. ;)
RE: Replacing Words from an Array by KooKas on 12-31-2008 at 12:09 PM

Ahah, thank you very much, Matti. Hopefully I won't make the mistake again :)


RE: Replacing Words from an Array by matty on 12-31-2008 at 03:21 PM

quote:
Originally posted by Matti
ChatWnd.EditText is the text currently being typed by the user. Because you're using the OnEvent_ChatWndSendMessage, this will be empty as the message is already about to be sent and therefore the input area has been cleared. To change the message to be sent, you need to return the modified message at the end of your function.
Everything you stated in your post is correct except for the statement I am quoting. During the OnEvent_ChatWndSendMessage function the pChatWnd.EditText property is still populated. It is cleared out once the function succeeds. Such things as the following is made possible because of this:

Javascript code:
function OnEvent_ChatWndSendMessage(pChatWnd, sMessage) {
    return pChatWnd.EditText.indexOf('(!VER)') === -1 ? sMessage : pChatWnd.EditText.replace('(!VER)', '(!VER)\nxxx');
}


The reason this is done like this is because Messenger Plus! will parse the text before passing it to the function therefore to get the native text in the Chat Window edit box it is done like that.

However the only problem with the above code is that the pChatWnd.EditText property is passed incorrectly therefore any emoticons to be sent (for instance: (!VER):P) will show up as (!VER). I don't know if Patchou is aware of this. I will mention it next time I see him online.

Cheers!



And also the code can be cut down:
Javascript code:
function OnEvent_ChatWndSendMessage(pChatWnd, sMessage) {
    var Greet1 = new Array('hi', 'hey', 'hello', 'helo');
    var Greet2 = new Array('sfe', 'wagwan', 'yo', 'oi');
 
    for (var x in Greet1) sMessage = sMessage.replace(Greet1[x], Greet2[x]);
 
    return sMessage;
}


RE: Replacing Words from an Array by KooKas on 12-31-2008 at 11:06 PM

Yeah, I normally do that when programming in ActionScript (a language very similar to J- and Java- script, but I wasn't really worrying about neatness, more about just getting it to work D: But yes, thank you, matty and Matti.