mIRC Homepage
Posted By: Raccoon Condensing a tokenized string - 04/07/18 06:51 AM
I have the string: X(1) X(2) X(4) Y(2) Y(3) Y(7) Z(4) Z(5) Z(6)

and I want to reduce it to: X(1)(2)(4) Y(2)(3)(7) Z(4)(5)(6)

Any suggestions?
Posted By: westor Re: Condensing a tokenized string - 04/07/18 07:23 AM
Try use that code:

Code:
alias fitall {
  if (!$1) { return }
  var %text = $1-
  var %t = $numtok(%text,32)
  var %i = 1
  while (%i <= %t) {
    var %v = $gettok(%text,%i,32)
    var %id = $left(%v,1)
    var %value = $right(%v,3)
    if (%id) { set -eu0 %fit_ $+ %id $addtok(%fit_ [ $+ [ %id ] ],%value,32) }
    inc %i
  }

  var %tot = $var(fit_*,0)
  var %z = 1
  while (%z <= %tot) { var %output = %output $right($var(fit_*,%z),-5) $+ $remove($var(fit_*,%z).value,$chr(32)) | inc %z }

  return $iif(%output,$v1,0)
}
Posted By: Wims Re: Condensing a tokenized string - 04/07/18 01:34 PM
I suppose you're not looking for westor's code as you could write something like it but rather an ugly $regsubex, this is the only thing that came to mind immediately

Quote:
//echo -a $regsubex(X(1) X(2) X(4) Y(2) Y(3) Y(7) Z(4) Z(5) Z(6),/([XYZ])(\(\d+\) (?:\1\(\d+\)(?= \1) )*\1\(\d+\))/g,\1 $+ $remove(\2,$chr(32),\1))
if you want it done with a static substitution, I don't think it's possible (or not without assuming too much) but people are surprising with regex...
Posted By: westor Re: Condensing a tokenized string - 04/07/18 10:49 PM
Originally Posted By: Wims
I suppose you're not looking for westor's code as you could write something like it but rather an ugly $regsubex, this is the only thing that came to mind immediately

Quote:
//echo -a $regsubex(X(1) X(2) X(4) Y(2) Y(3) Y(7) Z(4) Z(5) Z(6),/([XYZ])(\(\d+\) (?:\1\(\d+\)(?= \1) )*\1\(\d+\))/g,\1 $+ $remove(\2,$chr(32),\1))
if you want it done with a static substitution, I don't think it's possible (or not without assuming too much) but people are surprising with regex...


I FUCKING HATE REGEX !!!!!!!!!!!!!
Posted By: Raccoon Re: Condensing a tokenized string - 05/07/18 12:08 AM
Huh, interesting solutions, both of you. Thanks.
Posted By: jaytea Re: Condensing a tokenized string - 10/07/18 03:56 AM
hey Raccoon, this works for single letter function names: https://regex101.com/r/YrBB2y/1
Posted By: Raccoon Re: Condensing a tokenized string - 10/07/18 07:21 PM
Originally Posted By: jaytea
hey Raccoon, this works for single letter function names: https://regex101.com/r/YrBB2y/1
Code:
//echo -a $regsubex(X(1) X(2) X(4) Y(2) Y(3) Y(7) Z(4) Z(5) Z(6),/(?<=([A-Z]))\(\d+\)\K \1/g,)
> X(1)(2)(4) Y(2)(3)(7) Z(4)(5)(6)

I don't know why I didn't think about look-behinds, since they are indeed single letter and so static in length. Interesting! Thanks!
© mIRC Discussion Forums