|
hadar Apprentice
Joined: 30 Aug 2009 Posts: 198 Location: my apt, in california
|
Posted: 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 =/ |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: 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 |
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: 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. |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Mon Jan 30, 2012 9:35 pm |
You showed me the CDATA trick to parse the < and > signs correctly, though - thanks!
|
|
|
|
|
|
|
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
|
|