mIRC Homepage
Posted By: lonesome $sorttok - 08/04/03 05:45 AM
I know this has been posted quite alot of times, and i'm sure many of you scripters out there know about the negative numbers sorting bug. i've tried searching the forum but can't find what i want. any of you knows how to get a workabout? eg. a custom identifer $_sorttok ? I really have no idea how to make one.. smile thanks!
Posted By: qwerty Re: $sorttok - 08/04/03 09:08 AM
Code:
alias nsorttok {
  if - !isin $1 || $chr($2) !isin $1 { return $sorttok($1,$2,$iif($3 == r,nr,n)) }
  var %c = $regex(ns,$sorttok($1,$2,nr),/(.+?)-(.+)/)
  if $3 == r {
    !.echo -q $regsub($sorttok($regml(ns,2),45,n),/\D+(?!$)/g,$chr($2) $+ -,%c)
    return $gettok($+($regml(ns,1),$chr($2),-,%c),1-,$2)
  }
  !.echo -q $regsub($sorttok($regml(ns,2),45,nr),/\D+(?!$)/g,$chr($2) $+ -,%c)
  return $gettok($+(-,%c,$chr($2),$sorttok($regml(ns,1),$2,n)),1-,$2)
}

Usage: $nsorttok(<text>,C[,r])

If you specify "r" as the third parameter, it does a reverse sort

Examples:
$nsorttok(3 -2 -4 5 34,32)
$nsorttok(5#-2#-4#6,35,r)

Edit: minor modification that saves a few bytes of code (and cpu cycles)
Edit 2: 2nd minor modification that saves an extra few bytes :tongue:
Posted By: lonesome Re: $sorttok - 08/04/03 09:46 AM
woah. I knew it was out of my capability. I have no idea how to use $regsub. haha. Thankx anyway! smile
Posted By: lonesome Re: $sorttok - 08/04/03 09:51 AM
By the way, wad does "!.echo" do?
Posted By: lonesome Re: $sorttok - 08/04/03 09:55 AM
bug! there's something wrong.

$nsorttok(-1.-3.-5.-2.-4,46)
returns -4.-3.-2.-1.-5

some other results:
$nsorttok(-1.-3.-5.-2.-4.-7.-6.-8.-10.-11.-15.-16.-17.-9,46)
returns -17.-16.-15.-11.-10.-8.-7.-6.-5.-4.-3.-2.-1.-9
Posted By: qwerty Re: $sorttok - 08/04/03 10:03 AM
There is a little bug in there, which would made it work incorrectly if only negative numbers were given. To fix it, change this:
/(.+?)
to this:
/(.*?)
Posted By: lonesome Re: $sorttok - 08/04/03 10:12 AM
ah.. now it works! many thanks! =) laugh
Posted By: qwerty Re: $sorttok - 08/04/03 10:34 AM
The . (dot) is combined with the -q switch, to make /echo not print anything in any window but still evaluate the text passed to it. So, we make $regsub() work without using any temporary variable for its evaluation.

The ! makes sure the internal /echo is called and not a possible /echo alias. This way you avoid possible conflicts (from a poorly written /echo alias, for example) and also speed it up, as the internal /echo will always be significantly faster than the alias.
Posted By: lonesome Re: $sorttok - 08/04/03 10:44 AM
I see! Thanks for the enlightenment! laugh
Posted By: qwerty Re: $sorttok - 08/04/03 07:52 PM
The previous $nsorttok would screw up if the token separator is chr 46 (the "$"), because of PCRE's mysterious habit to consider "$" a special character, so it needs to be escaped (I can't see its usage in PCRE...). It also wouldn't work with decimal numbers. This version takes takes care of these issues:
Code:
alias nsorttok {
  if - !isin $1 || $chr($2) !isin $1 { return $sorttok($1,$2,$iif($3 == r,nr,n)) }
  var %c = $regex(ns,$sorttok($1,$2,nr),/(.*?)-(.+)/)
  if $3 == r {
    !.echo -q $regsub($sorttok($regml(ns,2),45,n),/^(\d+(?:\.\d+)?)/,\1 $+ $replace($chr($2),$,\$),%c)
    return $gettok($+($regml(ns,1),$chr($2),-,%c),1-,$2)
  }
  !.echo -q $regsub($sorttok($regml(ns,2),45,nr),/^(\d+(?:\.\d+)?)/,\1 $+ $replace($chr($2),$,\$),%c)
  return $gettok($+(-,%c,$chr($2),$sorttok($regml(ns,1),$2,n)),1-,$2)
}
Posted By: lonesome Re: $sorttok - 09/04/03 11:49 AM
ok thanks! smile
Posted By: RG_ Re: $sorttok - 10/04/03 01:08 AM
Here is an other alias for nsorttok.It is slow but it shows how to bubble sort an array.It has the same usage as qwerty's one.
Code:
alias nsorttok {
  var %tmp,%x,%y,%k,%n = $numtok($1,$2),%oper,%i = 1
  hmake sort 50
  %tmp = $gettok($1,%i,$2)
  while %tmp { hadd sort %i %tmp  | inc %i | %tmp = $gettok($1,%i,$2) }
  ;perform a bubble sort
  %i = 1
  %oper = $iif($3 = r,&lt;,&gt;)
  while %i &lt; %n {
    %k = 1
    while %k &lt; %n {
      %x = $hget(sort,%k)
      %y = $hget(sort,$calc(%k + 1))
      if %x %oper %y {
        hadd sort %k %y
        hadd sort $calc(%k + 1) %x
      }
      inc %k
    }
    inc %i
  }
  %i = 1
  %tmp =
  while %i &lt;= %n { %tmp = $+(%tmp,$chr($2),$hget(sort,%i)) | inc %i }
  hfree sort
  return %tmp
} 
© mIRC Discussion Forums