Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
hadar
Apprentice


Joined: 30 Aug 2009
Posts: 198
Location: my apt, in california

PostPosted: Mon Jan 30, 2012 7:03 pm   

Script Help
 
Im trying to make a function that in essence will let me do something like @colorother("this is a test")

where the function would then do
Code:
<color red>t</color><color blue>h</color><color red>i</color><color blue>s</color> <color red>i</color><color blue>s</color> <color red>a</color> <color blue>t</color><color red>e</color><color blue>s</color><color red>t</color>


everything i have tried has failed me =/
_________________
if you build it they will come, assuming that they have not already come to build it
Aardwolf Bootcamp
My youtube channel
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Mon Jan 30, 2012 8:43 pm   
 
Try this:
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="colorother">
    <value><![CDATA[$color = "red"
$words = %list( %-1, " ")
$out = ""
#FORALL $words {
  #LOOP %len( %i) {
    $out = %concat( $out, "<color ", $color, ">", %copy( %i, %j, 1), "</color>")
    #IF ($color = "red") {$color = "blue"} {$color = "red"}
    }
  $out = %concat( $out, " ")
  }
#RETURN %trim( $out)]]></value>
  </func>
</cmud>


Edit: XML requires literal < and > signs to be converted to & l t ; and & g t ; (without the spaces), but it seems the forum removes those... You'll need to change them back to paste in this code as XML, but it would probably just be easiest to make a function manually and copy the code in. Is there a workaround for this?


Last edited by Daern on Mon Jan 30, 2012 9:33 pm; edited 1 time in total
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Mon Jan 30, 2012 9:13 pm   
 
Ninja'd by Daern. His solution is much neater, though. ;)

Code:
#LOCAL String NewString Xterm RandomAmt
#IF (%2) {$RandomAmt=%2} {$RandomAmt=256}
$String=%subregex( %1, "(.)", "\1|")
$String=%replace($String,")",":cpar:")
$String=%replace($String,"(",":opar:")
$String=%replace($String, "@",":atsym:")
#FORALL $String {$Xterm=%random(1,$RandomAmt);$NewString=%concat($NewString,"@x",%if($Xterm<100,0),$Xterm,%i)}
#RETURN {%replace(%replace(%replace($NewString,":cpar:",")"),":opar:","("),":atsym:","@@")}


That's what I use for my rainbow script on Aard. To modify it to your case...

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="colorother" copy="yes">
    <value><![CDATA[#LOCAL String NewString Xterm RandomAmt
#IF (%2) {$RandomAmt=%2} {$RandomAmt=256}
$String=%subregex( %1, "(.)", "\1|")
$String=%replace( $String, ")", ":cpar:")
$String=%replace( $String, "(", ":opar:")
$String=%replace( $String, "@", ":atsym:")
$String=%replace($String, " ", " | ")
#LOOP %numitems($String) {#IF (!%mod(%i, 2)) {$NewString=%concat( $NewString,"<color red>", %item($String, %i), "</color>")} {$NewString=%concat($NewString, "<color blue>",%item($String,%i), "</color>")}}
$NewString=%subregex($NewString,"<color \w+>\s</color><color \w+>\s</color>"," ")
#RETURN {%replace( %replace( %replace($NewString, ":cpar:", ")"), ":opar:", "("), ":atsym:", "@@")}]]></value>
  </func>
</cmud>


Hope this helps. The :opar: and stuff is so CMUD can parse it correctly.
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Mon Jan 30, 2012 9:35 pm   
 
You showed me the CDATA trick to parse the < and > signs correctly, though - thanks!
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net