mIRC Homepage
Posted By: Zelda4ever32 $countcs - 19/02/05 10:35 PM
Hello, I am suggesting $countcs. workaround:
Code:
 alias countcs { return $calc(($len($1) - $len($removecs($1,$2))) / $len($2)) } 


-Zelda4ever
Posted By: tidy_trax Re: $countcs - 19/02/05 11:01 PM
Or:

Code:
alias countcs { return $regex($1,$+(/,$2/g)) }
Posted By: FiberOPtics Re: $countcs - 19/02/05 11:19 PM
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)

Code:
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.
Posted By: tidy_trax Re: $countcs - 19/02/05 11:42 PM
Indeed, I always forget about that wink
Yours messes up when trying to find \E btw:

Code:
//echo -a * $countcs(a,\E)


* 2
Posted By: FiberOPtics Re: $countcs - 20/02/05 12:17 AM
Hehe,

yes it does, also forgot to escape that hehe.
Posted By: tidy_trax Re: $countcs - 20/02/05 12:43 AM
Since \E is the only thing I found it messed up on, you could use a simple $iif():

Code:
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
}


Code:
//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:

Code:
//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:

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
}


grin

Edit 2:

I just thought of a way to escape \E:

Code:
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.
Posted By: FiberOPtics Re: $countcs - 20/02/05 12:58 AM
Oh my god LMAO wth was I thinking haha. Short answer: I wasn't grin

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.
© mIRC Discussion Forums