mIRC Homepage
Posted By: genius_at_work Token Identifiers - 14/11/05 07:45 PM
This could either be a feature suggestion or a code snippet. (Mods, move if necessary)

I recently had a use for a combination of token identifiers to get my code working. So instead of writing the same combination over and over, I made several custom identifiers. These are the ones I made:

Code:
$findwildtok(tokens,wildstring,N,M,C)
$repwildtok(tokens,wildstring,new,N,M,C)
$remwildtok(tokens,wildstring,N,M,C)


And here is the full code with comments:

Code:
; $findwildtok(tokens,wildstring,N,M,C)
; tokens = string to search in
; wildstring = wild string to search for
; N = which wild token to return
; M = which matching token to find
; C = token character
[color:green]findwildtok return $findtok($$1,$wildtok($$1,$$2,$$3,$$5),$$4,$$5)[/color] 
;
; $repwildtok(tokens,wildstring,new,N,M,C)
; tokens = string to search in
; wildstring = wild string to search for
; N = which wild token to return
; new = replacement string to insert
; M = which matching token to replace
; C = token character
[color:green]repwildtok return $reptok($$1,$wildtok($$1,$$2,$$3,$$6),$$4,$$5,$$6)[/color]
;
; $remwildtok(tokens,wildstring,N,M,C)
; tokens = string to search in
; wildstring = wild string to search for
; N = which wild token to return
; M = which matching token to replace
; C = token character
[color:green]remwildtok return $remtok($$1,$wildtok($$1,$$2,$$3,$$5),$$4,$$5)[/color]


If you have any suggestions/comments/improvements/etc, please post them. And like I said, if this post would be better in another section of the forum, mods can please move it.

-genius_at_work
Posted By: Riamus2 Re: Token Identifiers - 14/11/05 07:52 PM
Yes, I've done somewhat similar in the past. It would be a good addition, but probably will be relegated to just a snippet. You may want to submit it to some script sites as well as it is rather useful when you need to use those.

One minor suggestion. You don't need double $'s for every token identifier. If you just put a double $ on $5 (or $6 for the $reptok one), it will do the same thing because it will fail correctly no matter what number of tokens it has if it doesn't have 5 (or 6) tokens. Doesn't really hurt anything, but saves you a few bytes in your code. smile

Example:
remwildtok return $remtok($1,$wildtok($1,$2,$3,$$5),$4,$$5)

...or even:
remwildtok return $remtok($1,$wildtok($1,$2,$3,$5),$4,$$5)

The results are the same. smile
Posted By: qwerty Re: Token Identifiers - 15/11/05 01:22 AM
Actually this works only for aliases called as commands, not for custom identifiers. For example in $blah(,,,,a), $1 to $4 are $null but $5 is "a".
Posted By: DaveC Re: Token Identifiers - 15/11/05 08:27 AM
In all of them
The use of $$n in an identifier alias should be frowned apon, and $$n which does not exist well cause an HALT of the entire calling script, thats hardly the expected action of doing a $wildtok or a $findtok.
As an example you might be sent a line like !trigger \bolt \lock \latch \bolt \lid from this you wish to do $findwildtok($2-, \*, 1 , 1 , 32) or exactly $findwildtok(\bolt \lock \latch \bolt \lid, \*, 1 , 1 , 32), for that everything is ok but what if you recieved a line with just !trigger $2- is $null but the two funtions should be able to cope with this each returning a $null result.

I also didnt like the behavour if $3 used as the wildcard Nth matching counter was zero (total matches), it could cause abnormal/unexpected results. the wildmatch would return a value of total wildcard matches, that value would then be used as the actual matched text of the wildcard
As an example take $findwildtok(1 aa 2 ab 3 ac 4 ad 5 , a* , 0 , 1 , 32) a* matches 4 times and then the 1st occrance of 4 is the 7th token which doesnt match a* at all but is the result.

In rep and rem
If the wildtok fails to locate a matching token, the rep or rem tok recieves no token to match, and should return an unmodified string (IMO) however the actual action of rep and rem tok in these cases is to return no string at all.
examples $remwildtok(1 aa 2 ab 3 ac 4 ad 5 , ?e , 1 , 1 , 32) & $repwildtok(1 aa 2 ab 3 ac 4 ad 5 , ?e , 1 , XX , 1 , 32)
While this deviates from the normal behavour of token identifiers slightly, i feel it is an improvement, that should this occur the original string is returned


So heres what i offer...

alias findwildtok { return $iif(!$int($3),$wildtok($1,$2,$3,$5),$findtok($1,$wildtok($1,$2,$3,$5),$4,$5)) | :error | reseterror }
alias repwildtok { return $iif(!$int($3),$1,$iif($wildtok($1,$2,$3,$6) == $null,$1,$reptok($1,$v1,$4,$5,$6))) | :error | reseterror | return $1 }
alias remwildtok { return $iif(!$int($3),$1,$iif($wildtok($1,$2,$3,$5) == $null,$1,$remtok($1,$v1,$4,$5))) | :error | reseterror | return $1 }

The outter $iif checks if $3 is 0 and returns in the first instance the number of tokens wild matched, and in the other 2 the original string
In rep and rem should $3 have not been 0 the second $iif takes place which checks if the wild tok returns $null (but not zero), if so it returns the original string
else the calculation takes place.

Lastley i added some error trapping, I dont think they can error but why take the chance, returning $null on find, and the original text on the other two.
© mIRC Discussion Forums