mIRC Home    About    Download    Register    News    Help

Print Thread
#111903 19/02/05 10:35 PM
Joined: Apr 2004
Posts: 73
Z
Babel fish
OP Offline
Babel fish
Z
Joined: Apr 2004
Posts: 73
Hello, I am suggesting $countcs. workaround:
Code:
 alias countcs { return $calc(($len($1) - $len($removecs($1,$2))) / $len($2)) } 


-Zelda4ever

#111904 19/02/05 11:01 PM
Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
Or:

Code:
alias countcs { return $regex($1,$+(/,$2/g)) }


New username: hixxy
#111905 19/02/05 11:19 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
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)

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.


Gone.
#111906 19/02/05 11:42 PM
Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
Indeed, I always forget about that wink
Yours messes up when trying to find \E btw:

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


* 2


New username: hixxy
#111907 20/02/05 12:17 AM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
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.
#111908 20/02/05 12:43 AM
Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
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.

Last edited by tidy_trax; 20/02/05 01:16 AM.

New username: hixxy
#111909 20/02/05 12:58 AM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
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.

Last edited by FiberOPtics; 20/02/05 01:15 AM.

Gone.

Link Copied to Clipboard