mIRC Home    About    Download    Register    News    Help

Print Thread
#90089 11/07/04 07:33 PM
Joined: Apr 2004
Posts: 10
B
BaFe Offline OP
Pikka bird
OP Offline
Pikka bird
B
Joined: Apr 2004
Posts: 10
/help says:
$reptok(text,token,new,N,C)
Replaces the Nth matching token in text with a new token.

What about if there are more than 1 words that match the "token", and I want to replace everyone?
Nice upgrade would be something like if the N is 0 or null, it would replace every word that matches. (like $replace)
while loop is'nt a good trick, it might be infinite in some case.

If I'm blind or stupid and there is already something like this; please let me know smile

#90090 11/07/04 07:45 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
I totally agree with the suggestion, however, a while loop workaround is perfectly possible and doesn't lead to infinite loops. Here's a scripted version that works fine:
Code:
alias repalltok {
  var %i = $findtok($1,$2,0,$4), %a = $1
  while %i { 
    %a = $reptok(%a,$2,$3,1,$4) 
    dec %i 
  }
  return %a
}
Syntax is $repalltok(text,token,new,C)


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#90091 11/07/04 10:13 PM
Joined: Apr 2004
Posts: 10
B
BaFe Offline OP
Pikka bird
OP Offline
Pikka bird
B
Joined: Apr 2004
Posts: 10
hehe, thx.. script you gave kinda works.. Thats right, no infinite loops when using DEC no INC (you clever man)
this still bugs a bit eg: $repalltok(one hehe this bugs one really one,one,ONE ONE,32)
So it would surely be nice if Kamek could update the $reptok smile
Thx for your help qwerty o/

#90092 11/07/04 10:39 PM
Joined: Jan 2003
Posts: 3,012
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2003
Posts: 3,012
I'm sure an integrated version wouldn't do any better than qwerty's example. You are afterall adding another token into the sequence when checking. The intent is to replace one token with another token, not a series of them.


-KingTomato
#90093 12/07/04 02:42 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Ah, you're right, it was a mistake on my part, which is easily fixed though:
Code:
alias repalltok { 
  var %i = $findtok($1,$2,0,$4), %a = $1 
  while %i {
    %a = $reptok(%a,$2,$3,[color:red]%i[/color],$4)
    dec %i
  } 
  return %a
}
Here's another alternative that doesn't use loops at all (which makes it faster if the string contains many replaceable tokens)
Code:
alias repalltok2 {
  var %x = \x $+ $base($4,10,16,2)
  !.echo -q $regsub($gettok($1,1-,$4),/(?<=^| %x )\Q $+ $&
    $replacexcs($2,\E,\E\\E\Q) $+ \E(?=$| %x )/gix,$replace($3,\,\\,$,\$),%x)
  return %x
}


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#90094 12/07/04 08:57 AM
Joined: Apr 2004
Posts: 10
B
BaFe Offline OP
Pikka bird
OP Offline
Pikka bird
B
Joined: Apr 2004
Posts: 10
ok, points to you ;>
however, i should have figured the missing %i by myself blush
thx again, that'll do!


Link Copied to Clipboard