|
Joined: Apr 2004
Posts: 73
Babel fish
|
OP
Babel fish
Joined: Apr 2004
Posts: 73 |
Hello, I am suggesting $countcs. workaround: alias countcs { return $calc(($len($1) - $len($removecs($1,$2))) / $len($2)) } -Zelda4ever
|
|
|
|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
Or: alias countcs { return $regex($1,$+(/,$2/g)) }
New username: hixxy
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Actually, $count accepts multiple parameters where each N is added up to a total value of matches. I would propose something like this: Usage: $countcs(string,substring1,substring2,...,substringN) alias countcs {
var %a = 2, %b
while $($ $+ %a,2) != $null {
inc %b $regex($1,$+(/\Q,$v1,\E/g))
inc %a
}
return %b
} @tidy: as you know, there are a lot of chars that have a meaning inside a regex, which should be escaped. Without escaping, $countcs(test,.) would return 4, where it is clear it should be 0.
Gone.
|
|
|
|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
Indeed, I always forget about that  Yours messes up when trying to find \E btw: //echo -a * $countcs(a,\E) * 2
New username: hixxy
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Hehe,
yes it does, also forgot to escape that hehe.
Last edited by FiberOPtics; 20/02/05 12:59 AM.
|
|
|
|
Joined: Nov 2003
Posts: 2,327
Hoopy frood
|
Hoopy frood
Joined: Nov 2003
Posts: 2,327 |
Since \E is the only thing I found it messed up on, you could use a simple $iif(): alias countcs {
var %a = 2, %b
while $($ $+ %a,2) != $null {
inc %b $regex($1,[color:red]$iif($v1 == \E,/\\E/g,[/color]$+(/\Q,$v1,\E/g)[color:red])[/color])
inc %a
}
return %b
} //echo -a * $countcs(a,\E) | echo -a * $countcs(\E,\E) | echo -a * $countcs(\E\E,\E) * 0 * 1 * 2 Your bvar version, unfortunately, won't work: //echo -a * $countcs($chr(12),$chr(1)) * 1 The reason for this is that it finds 1 ($chr(1)) in 12 ($chr(12)) because binary variables hold ascii numbers. Edit: heh, my $iif version will still mess up on things like $countcs(a\E,\E\E), i'll go for the messy but working code: alias countcs {
var %i, %g = 2, %token, %n
while ($eval($+($,%g),2) != $null) {
%token = $v1
%i = 1
while ($mid($1,%i,$len(%token)) != $null) {
if ($v1 === %token) { inc %n }
inc %i
}
inc %g
}
return %n
}  Edit 2: I just thought of a way to escape \E: alias countcs {
var %a = 2, %b
while $($ $+ %a,2) != $null {
inc %b $regex($1,$+(/\Q,$replacecs($v1,\E,\E\\E\Q),\E/g))
inc %a
}
return %b
} Edit 3: I changed $replace to $replacecs in the above code, it seems to work flawlessly now.
Last edited by tidy_trax; 20/02/05 01:16 AM.
New username: hixxy
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,019 |
Oh my god LMAO wth was I thinking haha. Short answer: I wasn't  Edit nope, your last version of escaping \E doesn't work try on $countcs(\e,\e), that's why I decided to go for binvars, it's surprisingly more work than you'd originally expect, huh? After changing it to the cs version of $replace, it looks like we nailed it now lol.
Last edited by FiberOPtics; 20/02/05 01:15 AM.
Gone.
|
|
|
|
|