;; Random $replace by Sigh_
;;
;; Like $replace but replaces a random number of matched substrings
;; Usage: same as $replace i.e. $randreplace(string,substring,newstring,...)
;;
;; For example, $randreplace(abcabcabc,a,x,b,y,c,z)
;; Randomly replaces a with x, b with y and c with z
;;
;; Use the .cs property for case sensitivity -- $randreplace().cs
alias randreplace {
var %y = 2,%s = $1,%n = $+($,pos,$iif($prop = cs,cs),(%s,%r,%a))
while (%y < $0) {
var %r = $($ $+ %y,2),%a = 0,%t = $(%n,2),%x = $r(0,%t)
while (%x) {
var %a = $r(1,%t),%p = $(%n,2) - 1,%s = $&
$+($left(%s,%p),$($ $+ $calc(1+%y),2),$mid(%s,$calc(1+$len(%r)+%p)))
dec %t
dec %x
}
inc %y 2
}
return %s
}