mIRC Home    About    Download    Register    News    Help

Print Thread
Looking for this regsub function #126167 26/07/05 10:18 AM
Joined: Jul 2005
Posts: 2
Q
QuickStep Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
Q
Joined: Jul 2005
Posts: 2
Im looking for a regsub function that looks for a calculation identifier like $sin, $cos, $int etc. etc. and puts $calc inside the brackets, but only if there are no commas inside the brackets, few examples:
Code:
;Put $calc inside the functions:
$sin(2 + 3)  -->  $sin($calc(2 + 3))
$int(2) + 4  -->  $int($calc(2)) + 4

;If there are only brackets, it should stay this way:
(4 + 6) * 4  --> (4 + 6) * 6

;Functions with commas should stay the way they are:
$rand(2,6) * 6  -->  $rand(2,6) * 6

Any help, im not so good with regular expressions

Re: Looking for this regsub function #126168 26/07/05 11:12 AM
Joined: Aug 2003
Posts: 314
S
Sigh Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
Code:
alias addcalc {
  var %x
  .echo -q $regsub($1,/(\$(?!calc\50)[a-z\d]+)\(((?:[^,()]*\([^,()]*\))*[^,()]*)\)/gi,\1(\$calc(\2)),%x)
  return %x
}


Supports parentheses within these functions as long as there are no mismatches, for example: $addcalc( $!sin(1+1) $!tan((2+3)*5) ). Won't do anything with $calc itself

Re: Looking for this regsub function #126169 26/07/05 11:39 AM
Joined: Apr 2003
Posts: 701
K
Kelder Offline
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Here's a try:

alias addcalcs {
var %calc = $1-
var %re = /(\$(?:sin|tan|cos|int)\((?!\$calc))((?:[^,()]++|\([^)]++\))+)/g
var %sub = \1\$calc(\2)
while ($regsub(%calc,%re,%sub,%calc)) { }
if ($!isid) echo -a %calc
return %calc
}

I'll try to explain the regex itself (ignore whitespace below)

/
( \$ (?:sin|tan|cos|int) \( (?!\$calc) )
-> look for $ followed by sin or cos or tan or int followed by a ( followed by something else than $calc, and remember all that

( (?: [^,()]++ | \([^)]++\) )+ )
-> look for a sequence of characters that are not , or ( or )
-> or look for a sequence of ( followed by characters other than ) followed by a )
-> and do this multiple times, remember all that

/g


\1 \$calc( \2 )
And replace it with the first remembered part followed by $calc( second remembed part )

The ) of the $sin() isn't matched, so it won't need to be replaced.




Re: Looking for this regsub function #126170 26/07/05 12:16 PM
Joined: Aug 2004
Posts: 11
H
harl Offline
Pikka bird
Offline
Pikka bird
H
Joined: Aug 2004
Posts: 11
because i'm not too familiar with regular expressions yet, i don't know of a perfect solution using regex's and $regsub.
the ideas in the post above look fine to me though.

so here's just a fairly stupid workaround to test i thought of.
especially stupid, because $eval hasn't any parameter like the regex ()+ so evaluating a 200 times is as good (or bad) as 5 or 5000 here, not knowing what's exactly in $1-, and i don't see another manual (non-regex) way to have the returned "$calc($1-)" evaluated.
k, anyways..


Code:
alias hCheckpoint { return $iif(($chr(44) isin $1-),$1-,$eval($+($chr(36),calc,$chr(40),$1-,$chr(41)),200)) }

alias testRS {
  echo -at -- 12testrs on --
  echo -at $hCheckpoint($1-)

  ;-- echo -at $eval($+($1,$chr(40),$hCheckpoint($2-),$chr(41)),200)
  ;-- line above adds for sth like "testRS $sin 2+3", but obviously doesn't check if there're functions like $sin (where $1) used
  ;--which wouldn't support the commas, in case any of them are in $2-

  echo -at -- 04testrs end --
}


use /testRS

i also don't know if something like that can be combined with a regex effectively without adding to it, then again getting too intricate.

-harl-

Last edited by harl; 26/07/05 12:19 PM.
Re: Looking for this regsub function #126171 26/07/05 05:03 PM
Joined: Jul 2005
Posts: 2
Q
QuickStep Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
Q
Joined: Jul 2005
Posts: 2
Thanks for your replies its very much appreciated

First I went with Sigh's script, but unfortunately found a problem with nested functions, example:
[/code]
$sin(2 + 3 * $cos(3 * 6))
;would return
$sin($calc(2 + 3 * $cos(3 * 6)))
;and not
$sin($calc(2 + 3 * $cos($calc(3 * 6))))
[/code]

Then i went with Kelders snippet and everything seemed to work fine. So thanks again all of you and got it working now!

Last edited by QuickStep; 26/07/05 05:03 PM.