Random $replace

By Sigh_ on Dec 28, 2004

;; 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
}

Comments

Sign in to comment.
LIQUID_NiTrO   -  Dec 28, 2004

It\'s good, I just cant think of a practical use for it. =/

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.