Shoutbox

a problem scripting :/ [NOW SOLVED THANKS] - 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: a problem scripting :/ [NOW SOLVED THANKS] (/showthread.php?tid=71724)

a problem scripting :/ [NOW SOLVED THANKS] by Red King on 02-13-2007 at 10:14 PM

hey i'm kinda new to scripting but i knw one thing that should be pretty easy..yet it dont do it right..text replacement
some letters i try to have replaced work the way they should but many many others don't

the replacements go for example

Message = Message.replace(/a/gi,"(other wanted character)");

as the part that would replace a sertain piece of text

some of these work but most of my list just randomises the letter it becomes

i was trying to re-create the al bhed translators of final fantasy 10 into an msn script.. which i thougt i had working until i saw most of my letters were made wrong after i finished writing the script

what i was trying to make was
a b c d e f g h i j k l m n o p q r s t u v w x y z =
y p l t a v k r e z g m s h u b x n c d i j f q o w
as my wa of shortcutting to al bhed from the game

but instead when i test the finished script i'd end up with something rediculous like 3 S's 2 T's a few J's and maybe a Q where it belongs and everything lse is random scatter

can anyone help me out here? :S


RE: a problem scripting :/ by pollolibredegrasa on 02-13-2007 at 10:23 PM

It's because when you replace A with Y for example, when it comes to replace the letter Y it will then replace it again with the letter O. You're replacing letters multiple times...

A > Y > O

Not sure how best to explain it :s

Edit: see here


RE: a problem scripting :/ by Eljay on 02-13-2007 at 10:24 PM

What is probably happening is the following:
+Your script searches for any occurences of "a"
+Your script finds "a" and replaces it with "b"
+Your script searches for "b"
+Your script finds "b", including the "b" that originally was an "a", and replaces it with "c".

The better way to replace letters where there is a possibility of the replacement letter being replaced itself (if that makes any sense outside of my head) is to just loop through each letter in the string.

e.g.

code:
var teststring = "Hello World";
var testarray = teststring.split(""); //convert string into an array
for(i=0;i<testarray.length;i++){
  //do replacing here, get current character with testarray[i];
}
teststring = testarray.join(""); //convert array back to a string


RE: a problem scripting :/ by Red King on 02-13-2007 at 10:25 PM

but if that's how it goes then wouldn't the whole thing not do anything at all?
because if A>Y>O happens then it'll continue on to U> everything else going on a loop right?

edit:
so what you're saying is a text format code in the replacement character might work?
if it's not then that idea just randomly popped up and i think it's gonna rescur my script thanks ^^


RE: a problem scripting :/ by markee on 02-13-2007 at 10:39 PM

This is because you will just be doing one replace after the other on the one string. For example if you had the string "ABC" and replaced "A" with "B" and then "B" with "C" you will just end up with "CCC" rather than the "BCC" you were after.

To overcome this you will have to put the changes into a new variable.  This code should help with what you need (using what you have written).

code:
var alpha = "abcdefghijklmnopqrstuvwxyz";
var code = "ypltavkrezgmshubxncdijfqow";
var Split = Message.split("");
var eSplit = new Array();
for(i=0;i<Split.length;i++){
    for(j=0;j<alpha.length;j++){
        if(Split[i] == alpha.charAt(j)){
            eSplit[i] = code.charAt(j);
            break;
        }
if(j == alpha.length){
eSplit[i] = Split[i];
}
    }
}
var eMessage = eSplit.join("");
Debug.Trace(eMessage);

I hope this extract helps you.

EDIT: Forgot to pass other characters, all fixed now though.
RE: RE: a problem scripting :/ by Red King on 02-13-2007 at 10:48 PM

quote:
Originally posted by markee
This is because you will just be doing one replace after the other on the one string. For example if you had the string "ABC" and replaced "A" with "B" and then "B" with "C" you will just end up with "CCC" rather than the "BCC" you were after.

To overcome this you will have to put the changes into a new variable.  This code should help with what you need (using what you have written).

code:
var alpha = "abcdefghijklmnopqrstuvwxyz";
var code = "ypltavkrezgmshubxncdijfqow";
var Split = Message.split("");
var eSplit = new Array();
for(i=0;i<Split.length;i++){
    for(j=0;j<alpha.length;j++){
        if(Split[i] == alpha.charAt(j)){
            eSplit[i] = code.charAt(j);
            break;
        }
    }
}
var eMessage = eSplit.join("");
Debug.Trace(eMessage);

I hope this extract helps you.

thank you :)
i really appreciate it i'll try it out now

edit: i tried that and it's not replacing anything now is there anything else i'm supposed to add to all this?

RE: a problem scripting :/ by Matti on 02-14-2007 at 02:18 PM

quote:
Originally posted by Red King
edit: i tried that and it's not replacing anything now is there anything else i'm supposed to add to all this?
markee's code outputs the text in the debugging window. If you'd know how to script, you'd probably have seen that... :dodgy:

Anyways, replace
code:
Debug.Trace(eMessage);
with
code:
return eMessage;
according that you're working in the OnEvent_ChatWndSendMessage event.
RE: RE: a problem scripting :/ by Red King on 02-14-2007 at 08:47 PM

quote:
Originally posted by Mattike
quote:
Originally posted by Red King
edit: i tried that and it's not replacing anything now is there anything else i'm supposed to add to all this?
markee's code outputs the text in the debugging window. If you'd know how to script, you'd probably have seen that... :dodgy:

Anyways, replace
code:
Debug.Trace(eMessage);
with
code:
return eMessage;
according that you're working in the OnEvent_ChatWndSendMessage event.

alrighty then thanks for the info i'll try it now
aaand like i said before i don't know very well i'm kinda new
only started yesterday so i might be needing help for a while ^^;
i should soon get the hang of it though i learn most things kinda quick

edit: HURRAY thank you it works now it's just not giving me my spaces which i guess needs to be put in the character list or something so i'm about to do that now
thank you all guys for helping me out i promise i'll practice and make myself better so you aint stuck always helping me o.o;;

edit again XD: all done now and yes the no space problem was my A-Z's bot not having a space in the character list which is now done
thanks again guys i really appreciate your help

RE: a problem scripting :/ [NOW SOLVED THANKS] by markee on 02-14-2007 at 11:04 PM

I'll fix up my code above for you, I'm sorry for forgetting something like that.  It will just carry down any other characters.

EDIT: All fixed, and I'm sorry for not explaining the eMessage thing.  I just used that so as you still had the original message if you needed it for something.


RE: a problem scripting :/ [NOW SOLVED THANKS] by Red King on 02-14-2007 at 11:09 PM

well it's all fixed up and working fine on my ed
i fized it up with that little edit and added every chracter useable into the replacement list so they all work the same..
this is what the translation area has become instead of just a-z lower case

code:
var alpha = " !#~?/.>,<\|@':[{]}`£$%^&*()_+=-0987654321abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var code = " !#~?/.>,<\|@':[{]}`£$%^&*()_+=-0987654321ypltavkrezgmshubxncdijfqowYPLTAVKREZGMSHUBXNCDIJFQOW";