How do I do an output substitution?

Posted by Elvind on Sat 28 Jun 2008 08:27 PM — 17 posts, 65,359 views.

#0
Hi, I was wondering if someone could help me do a substitution in the text of in the output of the MUSH. I'm not great at coding, and I've been searching awhile for it.

anyway, what I'd like to know is how to replace a player's name with something else (for example, their real name, or a previous name they had) and all instances of that.

it would be greatly appreciated.

thank you! :D
Australia Forum Administrator #1
If you want to replace every instance, incoming from the MUD, install this small plugin.

Copy the text beween the lines and save as Convert_Word.xml.

Change the FROM word below to the name you want to match on, and the TO word to the one to change it to.

Then go to File menu -> Plugins and install it.

This will scan every incoming packet from the MUD and make the conversion. The %f stuff is to make sure only a whole word is converted. For example, since the FROM word is "foo" it won't match on "foot".

The conversion is case-sensitive.



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Convert_Word"
   author="Nick Gammon"
   id="9c0ed228bdb4a71c77e85ce0"
   language="Lua"
   purpose="Converts one word to another"
   date_written="2008-06-29"
   requires="4.00"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>

FROM = "foo"
TO = "bar"

function OnPluginPacketReceived (s)
  return (string.gsub (s, "%f[%a]" .. FROM .. "%f[%A]", TO))
end -- function OnPluginPacketReceived
</script>

</muclient>
#2
it works great! is there anyway it can substitute more than one word?

thanks so much already :D
#3
more than one instance... like say,

Jim -> Mike
Joe -> Bob
John -> Sam

etc...


I tried adding more in the FROM and TO, but it just substituted one.

thanks :D
Australia Forum Administrator #4
More than one, eh?


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Convert_Word"
   author="Nick Gammon"
   id="9c0ed228bdb4a71c77e85ce0"
   language="Lua"
   purpose="Converts one word to another"
   date_written="2008-06-29"
   requires="4.00"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>

conversions = {
  foo = "bar",
  dog = "cat",
  horse = "donkey",

  -- add more here
  }
  
function OnPluginPacketReceived (s)
  return (string.gsub (s, "%a+", conversions))
end -- function OnPluginPacketReceived
</script>

</muclient>


Amended on Sat 28 Jun 2008 10:55 PM by Nick Gammon
#5
great! :D thank you so much.
#6
This is an interesting plugin, very helpful. I've tried to do something similar myself but never been able to, since I'm quite inept at these things.

I have a few questions regarding this topic, if someone would be so kind as to point me in the right direction, I'd be grateful.

Is there someway to do exactly what this plugin does, but with entire sentences? (Able to do this -and- add colour to the subbed text?)

Example:
(From) You eat a sandwich (TO) Not hungry anymore!

Is there a way to make whatever you have targetted show up in caps?

Example:
t kobold -> kick kobold(input to MUD) -> You kick a KOBOLD(output on screen)

If anyone is able to give any feedback to this, I'm happy as can be. If this has been covered in another topic, I'm sorry but would be glad if someone would point me towards the topic if this is the case.

Thank you

EDIT: Doing a few searches and noticed this has been covered a few times(the 'sentence sub' at least, not the target in caps) and it seems like it's hard to do.
Amended on Mon 30 Jun 2008 09:51 AM by IneptGuest
Australia Forum Administrator #7
Quote:

Is there someway to do exactly what this plugin does, but with entire sentences? (Able to do this -and- add colour to the subbed text?)


The best way is to make a trigger, omit the triggered line from output, and then do a ColourNote to send whatever replacement line you want, in whatever colour you want.

Ditto for changing a word like kobold.
#8
Ah, thank you!

Quote:
Ditto for changing a word like kobold.


It's not the word I'm trying to get in caps, it's whatever I have in the @target variable. Sorry if described it poorly.

Australia Forum Administrator #9
I suggest highlighting the word you want in a eye-catching colour. This trigger demonstrates the idea:


<triggers>
  <trigger
   custom_colour="17"
   enabled="y"
   expand_variables="y"
   match="@target"
   regexp="y"
   sequence="100"
   other_text_colour="orangered"
   other_back_colour="yellow"
  >
  </trigger>
</triggers>


With "expand_variables" checked, it matches on whatever word is in the 'target' variable, anywhere on the line, and colours it red on yellow.
#10
A highlight would work just as well, of course. Thanks a lot!
#11
Is there any way to subsitute few words to few diffrent words for instance: "i am great" = "i hate myself"?
Australia Forum Administrator #12
One approach is to do it at the packet level, but I warn you that the output from the MUD could quite easily be broken over multiple packets, and so this may not work.

Using my earlier plugin above, you could do something like this. Change:


 return (string.gsub (s, "%a+", conversions))


to:


 s = string.gsub (s, "i am great", "i hate myself")
 s = string.gsub (s, "fish is good", "seafood is great")
 return s


However as I said, this depends on everything being in one packet, and also that there is no telnet codes (like colour codes) in there.

Another approach is to make a trigger, omit that line from output, and then redisplay it with the substituted text. That will work regardless of how many packets the line takes, and at that stage the color information has been stripped out.
#13
Hello Nick! I appreciate all your great work on this plugin (and all the others)

In this convert_word plugin, is it possible to convert words that are variables?

meaning, variable @char1 is "Peter"

I want to convert Peter to "Peter-[1]"

When I change the contents of my variable to say Pierre, it would then convert Pierre to "Pierre-[1]"

I tried @char1="@char1-[1]" but it did not work...

awaiting response...
Australia Forum Administrator #14
You could do something like:



conversions [ GetVariable ("char") ] = GetVariable ("char") .. "-[1]"


So this pulls in the contents of the variable "char", puts it into the table as the match text, and also the replacement text with "-[1]" after it.
#15
Thanks for the fast reply, but my understanding of syntaxes is still at newbie level so I need further help how to insert it.


Quote:

<script>

conversions = {
[GetVariable ("char1")] = GetVariable ("char1").."-[1]",
[GetVariable ("char2")] = GetVariable ("char2").."-[2]"
}
function OnPluginPacketReceived (s)
return (string.gsub (s, "%a+", conversions))
end -- function OnPluginPacketReceived
</script>


this above gives error parsing line and doesn't load the plugin.
Australia Forum Administrator #16
Where are these variables? In the main world file? If so you need to change it to pull in the main variables like this:


conversions = {
[GetPluginVariable ("", "char1")] = GetPluginVariable ("", "char1").."-[1]",
[GetPluginVariable ("", "char2")] = GetPluginVariable ("", "char2").."-[2]"
}


The GetPluginVariable call, using plugin "", means get char1 and char2 contents from the "main world" not the current plugin.

These variables will need to exist before you load the plugin.