What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Replacing Words from an Array

Replacing Words from an Array
Author: Message:
KooKas
New Member
*


Posts: 3
Joined: Dec 2008
O.P. Replacing Words from an Array
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)
{
}

12-31-2008 07:18 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Replacing Words from an Array
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. ;)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
12-31-2008 09:29 AM
Profile E-Mail PM Web Find Quote Report
KooKas
New Member
*


Posts: 3
Joined: Dec 2008
O.P. RE: Replacing Words from an Array
Ahah, thank you very much, Matti. Hopefully I won't make the mistake again :)
12-31-2008 12:09 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Replacing Words from an Array
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;
}


This post was edited on 12-31-2008 at 03:41 PM by matty.
12-31-2008 03:21 PM
Profile E-Mail PM Find Quote Report
KooKas
New Member
*


Posts: 3
Joined: Dec 2008
O.P. RE: Replacing Words from an Array
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.

This post was edited on 12-31-2008 at 11:07 PM by KooKas.
12-31-2008 11:06 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On