mIRC Home    About    Download    Register    News    Help

Print Thread
$sorttok #18483 08/04/03 05:45 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
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!

Re: $sorttok #18484 08/04/03 09:08 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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:


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: $sorttok #18485 08/04/03 09:46 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
woah. I knew it was out of my capability. I have no idea how to use $regsub. haha. Thankx anyway! smile

Re: $sorttok #18486 08/04/03 09:51 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
By the way, wad does "!.echo" do?

Re: $sorttok #18487 08/04/03 09:55 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
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

Re: $sorttok #18488 08/04/03 10:03 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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:
/(.*?)


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: $sorttok #18489 08/04/03 10:12 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
ah.. now it works! many thanks! =) laugh

Re: $sorttok #18490 08/04/03 10:34 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: $sorttok #18491 08/04/03 10:44 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
I see! Thanks for the enlightenment! laugh

Re: $sorttok #18492 08/04/03 07:52 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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)
}


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: $sorttok #18493 09/04/03 11:49 AM
Joined: Dec 2002
Posts: 62
lonesome Offline OP
Babel fish
OP Offline
Babel fish
Joined: Dec 2002
Posts: 62
ok thanks! smile

Re: $sorttok #18494 10/04/03 01:08 AM
Joined: Dec 2002
Posts: 19
R
RG_ Offline
Pikka bird
Offline
Pikka bird
R
Joined: Dec 2002
Posts: 19
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
}