|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
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..  thanks!
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
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
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
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!
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 62 |
By the way, wad does "!.echo" do?
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
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
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
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
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 62 |
ah.. now it works! many thanks! =)
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
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
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 62 |
I see! Thanks for the enlightenment!
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
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: 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
|
|
|
|
Joined: Dec 2002
Posts: 62
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 62 |
ok thanks!
|
|
|
|
Joined: Dec 2002
Posts: 19
Pikka bird
|
Pikka bird
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. 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,<,>)
while %i < %n {
%k = 1
while %k < %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 <= %n { %tmp = $+(%tmp,$chr($2),$hget(sort,%i)) | inc %i }
hfree sort
return %tmp
}
|
|
|
|
|